Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 变量赋值之间的差异? 假的 0 空的 未定义 空字符串_Javascript_Null_Undefined - Fatal编程技术网

Javascript 变量赋值之间的差异? 假的 0 空的 未定义 空字符串

Javascript 变量赋值之间的差异? 假的 0 空的 未定义 空字符串,javascript,null,undefined,Javascript,Null,Undefined,我使用它们,但我仍然不知道上面列表中的每个潜在客户都有边际差异。 我主要使用0,false。但是我遇到过许多使用未定义的空字符串的脚本。 我想知道他们之间的确切区别。 我知道这是一个愚蠢的问题,但如果我能得到一个简单的答案,那就太好了。类型就是区别 false是布尔值,0是数字,null是对象,undefined是未定义的,'是字符串 您可以根据所使用的类型来选择类型。例如: // There is nothing wrong with this block of code var num_ca

我使用它们,但我仍然不知道上面列表中的每个潜在客户都有边际差异。 我主要使用0,false。但是我遇到过许多使用未定义的空字符串的脚本。 我想知道他们之间的确切区别。
我知道这是一个愚蠢的问题,但如果我能得到一个简单的答案,那就太好了。

类型就是区别

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 
}
两个最容易混淆的伪值可能是
null
未定义的

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

它被称为“truthy and falsy values”如果你想知道如何参考它。 以下是一个链接,用于解释您问题的答案:
(阅读开头的链接时请记住!!(值)强制值为true或false)

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

  • 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;
}
在这种情况下,不会传递
by
参数,因此JavaScript将其作为
undefined
隐式传递。不过,我不建议将其分配给变量;相反,我会使用
null

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

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

  • 您拥有的列表是javascript中6个“falsy”值中的5个。如果添加“NaN”,您将拥有javascript中的所有falsy值。因此完整的“falsy”列表是

    在“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语句中的所有falsy值是否彼此相等(=)

    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){ console.log(“将无法访问”); }


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

    请查看
    user2549366
    的答案。帮助了我。我认为有比该链接更好的答案。
    //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(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');
    }
    
    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");
    }