JS的相等比较运算与类型转换

在JavaScript中,相等有两种比较方式:普通相等比较(==,在ECMAScript标准中称为“抽象相等比较”)和 严格相等比较(===)。
对于被广泛使用的普通相等比较运算符来讲,不同类型的操作数在进行比较之前,会尝试将两个操作数转换成相同的类型,再作比较运算。而对于严格相等比较运算符来讲,仅当两个操作数的类型相同且值相等时,返回为true。

抽象相等比较

ECMAScript标准的算法细节如下:

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
    1. Return the result of performing Strict Equality Comparison x === y.
  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
  10. Return false.

翻译如下:
1.如果Type(x)与Type(y)相同,执行严格相等运算x === y;
2.如果x是null,y是undefined,返回true;
3.如果x是undefined,y是null,返回true;

console.log(null == undefined);  

你可能感兴趣的:(前端日志,相等,比较,类型转换,javascript,运算)