JavaScript中==和===运算符之间的区别

在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。

== vs ===的示例

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