美文网首页
if 语句判断的三个等号

if 语句判断的三个等号

作者: fejavu | 来源:发表于2019-04-09 23:36 被阅读0次

一个等号

在直觉和英语语义的理解中,如果要判断 a 是不是等于 b,直接的翻译应该就是

if(a = b) {
  //do something
}

但是在 JavaScript 语言和其他编程语言里面,等号"="是一个操作符,它将它右边的值赋予左边的变量,是一个操作。因此,如果放在判断语句里面,他首先会执行一个赋值操作,将 a 的值改变成 b,然后判断 if 语句是否执行,这样的话相当于判断 b 的值(不讨论涉及类型转换),这完全违背了判断的原意,例如下面一个函数:

function judge() {
  var a = 1;
  var b = 2;
  if(a = b) {
    console.log('a equals to 2.');
  }else {
    console.log('a is still 1.');
  }
}  // "a equals to 2."

MDN 文档中提到,如果编程的人本意确实是,先赋值后判断的话,从代码可读性上来说,也应该加入一个括号,表示先进行复制,再进行赋值后的变量判断。

如果你需要在条件表达式中使用赋值,通常在赋值语句前后额外添加一对括号。例如:

if ((x = y)) {
  /* do the right thing */
}

两个等号

这是 ECMA 关于 两个等号的规范

当我们要做正确的判断的时候应该使用两个等号,判断左右两个值是否相等,例如:

if (a == b) {
  console.log('a equals to b.');
} else {
  console.log('a does not equal to b');
}

这样的一个判断语句才是判断 a 的值是否等于 b 的,当 a,b 相等的时候,if判断语句返回 true ,从而执行 if 条件内的语句,否则执行 else 条件的语句。

然而,两个等号依然不是做好的做两个值判断的最好方式,因为 JavaScript 中存在对变量类型的强制转换,例如下面一个函数:

function judge() {
  var a = 16;
  var b = '16';
  if(a == b) {
    console.log('a equals to b.');
  }else {
    console.log('a does not equal to b.');
  }
}  // "a equals to b."

a 是 number 类型的 16,b 是 string 类型的 '16',当使用两个等号判断它们相等的时候,从类型上它们是不相等的,但是由于 JavaScript 的类型转换, b 被转换成 number 与 a 进行判断。因此,原本不相等的两个变量却相等了,这是一种不严谨的判断方式,那么有没有更严谨的判断方式呢?

有,三个等号。

三个等号

三个等号是判断两个值是否严格相等的判断语句,它首先判断两边的数据类型,如果两边的数据类型不相等,直接 return false,而无需做数值判断。具体的判断规则,ECMA 规范中,一共有七条:

The Strict Equality Comparison Algorithm

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then:
    If x is NaN, return false.
    If y is NaN, return false.
    If x is the same Number value as y, return true.
    If x is +0 and y is −0, return true.
    If x is −0 and y is +0, return true.
    Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false.

其中比较重要的有几条:

  1. 当 x 和 y 均的等于 undefined 或者 null 时,则 return true;
  2. 当 x 和 y 属于 number 类型时,数值相等,两者是正负0时,则 return true ;当两者不等或者 是 NaN 时,则 return false,NaN 不和自己相等
  3. 当两者类型为 string时,只有两个字符串长度相等且字符都是相同位置时才 return true。
  4. 当 x 和 y 是 object 类型时,只有 x 和 y 是指向同一个 object 时,才return true ,其他则 return false。

第四条中,即便两个object 赋值相同的 key ,value ,两者也不相等。如:

function equalObj() {
  var obj1 = {'key':'value'};    
  var obj2 = {'key':'value'};
  var obj3 = obj1;
  
  if(obj1 === obj2) {
    console.log('obj1 strictly equals to obj2.');
  }else if (obj1 === obj3) {
    console.log('obj1 strictly equals to obj3.');
    console.log('obj1 dose not equals to obj2.');
  }
}  //"obj1 strictly equals to obj3."  "obj1 dose not equals to obj2."

相关文章

网友评论

      本文标题:if 语句判断的三个等号

      本文链接:https://www.haomeiwen.com/subject/smdwiqtx.html