在JavaScript中,双精度和三重等于用于两个操作数之间的比较。两者之间的区别是:
序号 | 键 | 双重等于(==) | 三重等于(===) |
---|---|---|---|
1个 | 命名 | Double equals named as Equality Operator. | 三等号称为Identity / Strict等式运算符。 |
2 | 比较方式 | Double equals used as Type converting the conversion | 三重等于用作严格转换,而不对操作数执行任何转换。 |
3 | 语法 | Double equals has syntax for comparison as (a == b) | 三等号的比较语法为(a === b) |
4 | 实作 | Double equals first convert the operands into the same type and then compare i.e comparison would perform once both the operands are of the same type. This is also known as type coercion comparison. | 另一方面,三元等于在比较之前不执行任何类型的转换,并且仅在两个操作数的类型和值完全相同时才返回true。 |
Equals.jsp
var a = true; var b = 1; var c = true; console.log (a == b); // first convert 1 into boolean true then compare console.log (a === c); // both are of same type no conversion required simple compare. console.log (a === b); // no conversion performed and type of both operands are not of same type so expected result is false.
输出结果
true true false