关于js中的 “==” 与 “===”

在js中,'==' 和 '==='运算符用来比较两个值是否相等,可是他们对于相等的定义是不一样的。两个运算符均可以用来比较任意类型的操做数,若是两个操做数相等,返回true,不然,返回false。'===' 严格相等运算符,用来比较两个操做数是否严格相等。'==' 相等运算符,用来比较两个操做数是否相等。
详细信息可参照ECMA标准(戳这里)。翻译

Abstract Equality Comparison ==

== 相等操做符,在比较前会把比较的两个数转换成相同的数据类型以后,而后对两个数进行比较。转换后,比较方式与 === 相同。code

ECMA中比较规则以下:orm

The comparison x == y, where x and y are values, produces true or false.

1. ReturnIfAbrupt(x).
2. ReturnIfAbrupt(y).
3. If Type(x) is the same as Type(y), then
    Return the result of performing Strict Equality Comparison x === y.
4. If x is null and y is undefined, return true.
5. If x is undefined and y is null, return true.
6. If Type(x) is Number and Type(y) is String,
    return the result of the comparison x == ToNumber(y).
7. If Type(x) is String and Type(y) is Number,
    return the result of the comparison ToNumber(x) == y.
8. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
9. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then
    return the result of the comparison x == ToPrimitive(y).
11. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then
    return the result of the comparison ToPrimitive(x) == y.
12. Return false.

翻译以下:对象

比较 x == y,当x 和 y是正常值时,返回 true 或者 false。索引

  1. 若是x不是正常值(好比抛出一个错误),中断执行。
  2. 若是y不是正常值,中断执行。
  3. 若是Type(x)与Type(y)相同,执行严格相等运算x === y。
  4. 若是x为null且y为undefined,则返回true。
  5. 若是x为undefined,y为null,则返回true。
  6. 若是Type(x)是Number,Type(y)是String,返回比较结果 x == ToNumber(y)。
  7. 若是Type(x)是String,Type(y)是Number,返回比较结果ToNumber(x)== y。
  8. 若是Type(x)为Boolean,则返回比较结果ToNumber(x)== y。
  9. 若是Type(y)为Boolean,则返回比较结果x == ToNumber(y)。
  10. 若是Type(x)为String,Number或Symbol,Type(y)为Object,则返回比较的结果x == ToPrimitive(y)。
  11. 若是Type(x)是Object,Type(y)是String,Number或Symbol,那么
    返回比较结果ToPrimitive(x)== y。
  12. 返回假。

简化一下 ,能够理解为:it

  1. 若是两个操做数类型相同,则进行 x===y。
  2. 若是一个为null,另外一个为undefined,则返回true。
  3. 若是两个操做数均为基本数据类型,则把操做数转换为Number类型进行比较。
  4. 若是其中有一个操做数为Object,则调用对象的 toString 或者 valueOf 方法,将对象转化为原始值进行比较。
  5. 若是不知足上述任何状况,则返回 false。

Strict Equality Comparison '==='

'===' 严格相等操做符,用来比较两个操做数是否严格相等。form

ECMA中比较规则以下:数据类型

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
    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.
5. If Type(x) is String, then
    a. If x and y are exactly the same sequence of code units (same length and same code           units at corresponding indices), return true.
    b. Else, return false.
6. If Type(x) is Boolean, then
    a. If x and y are both true or both false, return true.
    b. Else, return false.
7. If x and y are the same Symbol value, return true.
8. If x and y are the same Object value, return true.
9. Return false.

翻译:方法

  1. 若是Type(x)与Type(y)不一样,则返回false。
  2. 若是Type(x)为Undefined,则返回true。
  3. 若是Type(x)为Null,则返回true。
  4. 若是Type(x)是Number,那么im

    1. 若是x是NaN,则返回false。
    2. 若是y是NaN,则返回false。
    3. 若是x与y的Number值相同,则返回true。
    4. 若是x为+0且y为-0,则返回true。
    5. 若是x是-0而y是+0,则返回true。
    6. 返回假。
  5. 若是Type(x)是String,那么

    1. 若是x和y是彻底相同的代码单元序列(相同长度和相应索引处的相同代码单位),则返回true。
    2. 不然返回假。
  6. 若是Type(x)为Boolean,则
    a.若是x和y都为true或都为false,则返回true。
    b.不然返回假。
  7. 若是x和y是相同的符号值,则返回true。
  8. 若是x和y是相同的Object值,则返回true。
  9. 返回假。

简化一下,能够理解为:

  1. 若是两个操做数类型不相同,返回false。
  2. undefined === undefined => true
  3. null === null => true
  4. 若是操做数的数据类型都为Number,当两个数的值相同时,返回true, 不然返回 false。

    注: -0 === +0   => true     +0 === -0 => true
        NaN 与任何值都不相等,包括他本身。 因此要判断一个数值是否为NaN, 可采用 x !== x ,只有NaN 返回true
  5. 若是操做数的数据类型都为String或Boolean时,只有x和y彻底相同,返回ture。
  6. 若是操做数的数据类型都为Object,只有两个操做数指向的地址彻底相同时,返回true,不然返回false。
相关文章
相关标签/搜索