Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript双等与三等的布尔值比较 #44

Open
yanyue404 opened this issue Dec 20, 2018 · 0 comments
Open

Javascript双等与三等的布尔值比较 #44

yanyue404 opened this issue Dec 20, 2018 · 0 comments

Comments

@yanyue404
Copy link
Owner

yanyue404 commented Dec 20, 2018

说明

面试被一连串的 考基础的双等与三等判断问的有点蒙,虽然知道== 是抽象相等运算符, === 是严格相等运算符,但其中的比较规则并不胜了解,那么为何[] == [] true呢 ?其他规则是如何 ?

知乎提问中找到了答案, 感谢,一起来看一下ECMAScript 6 官方文档

双等比较

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 the same as Type(y), then Return the result of performing Strict Equality Comparison x === y.
[] == []; // false
  1. If x is null and y is undefined, or x is undefined and y is null, return true.
null == undefined; // ture
  1. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y), vice versa (反之亦然).
1 == '1'; // true
0 == ''; // true 
0 == '0'; // true

Number("") => 0

  1. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y, vice versa.
0 == false; // true
1 == true; // true
  1. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y), vice versa.

  2. Return false.

三等比较

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.
1 === "1"; // false
  1. If Type(x) is Undefined, return true. (按顺序执行,x 与 y 须类型一致,以下一样除了 第9)
  2. If Type(x) is Null, return true.
  3. If Type(x) is Number, then
   a. If x is NaN, return false.
   b. If y is NaN, return false.
   c. If x is the same Number value as y, return true.
   d. If x is +0 and y is −0, return true.
   e.If x is −0 and y is +0, return true.
   f. Return false.
  1. If Type(x) is String, then
  a. If x and y are exactly the same sequence of code units (same length and same code unit
      at corresponding indices), return true.
  b. Else, return false.
  1. If Type(x) is Boolean, then
  a. If x and y are both true or both false, return true.
  b. Else, return false.
  1. If x and y are the same Symbol value, return true.
  2. If x and y are the same Object value, return true.
  3. Return false.

总结

== 运算符有可能是在进行必要的类型转换后,才再比较。=== 运算符不会进行类型转换,所以如果两个值不是相同的类型,会直接返回false。使用== 时,可能发生一些特别的事情,例如:

1 == '1'; // true
1 == [1]; // true
1 == true; // true
0 == ''; // true
0 == '0'; // true
0 == false; // true

建议是从不使用 == 运算符,除了方便与 null 或undefined比较时,a == null如果a为null或undefined将返回true。

var a = null;
console.log(a == null); // true
console.log(a == undefined); // true

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant