在js中,'==' 和 '==='运算符用来比较两个值是否相等,可是他们对于相等的定义是不一样的。两个运算符均可以用来比较任意类型的操做数,若是两个操做数相等,返回true,不然,返回false。'===' 严格相等运算符,用来比较两个操做数是否严格相等。'==' 相等运算符,用来比较两个操做数是否相等。
详细信息可参照ECMA标准(戳这里)。翻译
== 相等操做符,在比较前会把比较的两个数转换成相同的数据类型以后,而后对两个数进行比较。转换后,比较方式与 === 相同。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。索引
简化一下 ,能够理解为:it
'===' 严格相等操做符,用来比较两个操做数是否严格相等。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.
翻译:方法
若是Type(x)是Number,那么im
若是Type(x)是String,那么
简化一下,能够理解为:
若是操做数的数据类型都为Number,当两个数的值相同时,返回true, 不然返回 false。
注: -0 === +0 => true +0 === -0 => true NaN 与任何值都不相等,包括他本身。 因此要判断一个数值是否为NaN, 可采用 x !== x ,只有NaN 返回true