Computer >> Computer tutorials >  >> Programming >> Javascript

Difference between == and === operator in JavaScript


In JavaScript, the double and triple equals are used for comparison between two operands. The difference between both the equals is:

Sr. No.KeyDouble Equals (==)Triple Equals (===)
1NamingDouble equals named as Equality Operator.Triple equals named as Identity / Strict equality Operator.
2ComparisonDouble equals used as Type converting the conversionTriple equals used as Strict conversion without performing any conversion in operands.
3SyntaxDouble equals has syntax for comparison as (a == b)Triple equals has syntax for comparison as (a === b)
4ImplementationDouble 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.On the other hand, triple equals do not perform any type of conversion before comparison and return true only if type and value of both operands are exactly the same.

Example of == 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.

Output

true
true
false