赋予变量的赋值之间的差异?

时间:2013-07-04 07:05:27

标签: javascript null undefined

  1. 0
  2. 未定义
  3. 空字符串
  4. 我使用它们,但我仍然没有意识到上面列表中每个潜在客户的边际差异。 我大多使用0,false。但是我遇到过许多使用未定义的空字符串的脚本。 我想知道它们之间的确切差异。 我知道这是一个愚蠢的问题,但如果我得到一个小小的答案就会很棒。

4 个答案:

答案 0 :(得分:2)

如果你想知道如何引用它,它被称为“真值和假值”。 这是一个解释您问题答案的链接:http://www.sitepoint.com/javascript-truthy-falsy/ (在开头读取链接时请记住!!(值)强制值为true或false)

答案 1 :(得分:0)

类型不同。

false是布尔值,0是数字,null是对象,undefined是未定义的,''是字符串。

您可以根据使用的内容选择类型。例如:

// There is nothing wrong with this block of code
var num_cats = 7;
if(num_cats){
    // num_cats is truthy
}

// This block works but it could be made more clear
var has_cat = num_cats;
if(has_cat){
    // This would work, but it doesn't make sense. As a developer I would
    // expect that has_cat should be either true or false, not 7. 
    // One way to convert the number to a boolean would be:
        has_cat = !!num_cats 
}

两个最令人困惑的假名值可能是nullundefined

null基本上意味着变量存在,但它的值是未知的。 undefined表示变量不存在(尽管变量可以明确设置为未定义,如var x = undefined;,然后变量x存在,但明确没有定义,这意味着你可以把它视为不存在。

答案 2 :(得分:0)

您提到的只有两个值是我称之为“特殊值”的值。

  • null - 在大多数情况下,这相当于不适用。
  • undefined - null
  • 的隐含版本

两者的一个例子:

function findByTitle(arr, title)
{
    for (var i = 0; i < arr.length; ++i) {
        if (arr[i].title === title) {
            return arr[i];
        }
    }
    return null;
}

null的返回值表示找不到记录,否则它是一个对象。

function increment(x, by)
{
    return x + (by || 1); // by may be undefined
}

increment(4); // 5

在这种情况下,by参数未传递,因此JavaScript会将其作为undefined隐式传递。我不建议将其赋值给变量;相反,我会使用null

你提到的其他价值并不是特别特别;它们可以用作起始值,例如构建字符串值或计算总和,但它们本身并不特殊。

  • ""是一个字符串
  • false是布尔值
  • 0NaN是数字

答案 3 :(得分:0)

您拥有的列表是javascript中6个“虚假”值中的5个。如果你添加“NaN”,你将拥有javascript中的所有虚假值。所以完整的“假”列表是

1)0
2)""
3)false
4)undefined
5)null and
6)NaN

在“if”语句中使用时,这些都表现相同

if(0)
if("")
if(false)
if(undefined)
if(null) and
if(NaN) would behave the same

您要求简短的答案,但我认为最好的方法是展示它如何与一些基本测试一起使用。为长期答案道歉

//Checking if all the "falsy" values evaluate the same way in a "if" statement 
console.log("Checking if(0)");
if(0) {
   console.log(" if(0) Will not be reached");
}

console.log('Checking if("")');
if("") {
   console.log(' if("") Will not be reached');
}

console.log("Checking if(undefined)");
if(undefined) { 
   console.log("if(undefined) Will not be reached");
}

console.log("Checking if(null)");
if(null) {
   console.log("if(null) Will not be reached");
}

console.log("Checking if(Nan)");
if(NaN) {
   console.log("if(NaN) Will not be reached");
}

console.log("Checking if(false)");
if(false) {
   console.log("if(false) Will not be reached");
}

//在if语句中检查所有假值是否相等(==)

if(0 == "") {
  console.log('if(0 == "") is true');
}

if(0 == false) {
  console.log("if(0 == false) is true");
}

if("" == false) {
  console.log('if("" == false) is true');
}

if(0 == undefined) {
  console.log("if(0 == undefined) Will not be reached");
}

if("" == null) {
  console.log('if("" == null) Will not be reached');
}

if(undefined == null) {
  console.log("if(undefined == null) is true");
}

if(NaN == "") {
  console.log('if(NaN == "") Will not be reached');
}

//检查false和falsy值之间的严格相等     if(undefined === false){       console.log(“不会到达”);     }

if(null === false) {
  console.log("Will not be reached");
}

if(undefined ===false) {
  console.log("Will not be reached");
}

if(0 === false) {
  console.log("Will not be reached");
}

if("" === false) {
  console.log("Will not be reached");
}

if(NaN === false) {
  console.log("Will not be reached");
}

这意味着虽然这些“假”值可以在“if”语句中互换使用,但它们彼此不相等(==)(特别是0的集合,“”和其他的假三)。如果使用更严格的等于(====),则这些都不等于false,因此可能分类为“falsy”而不是false。