Equality in Javascript == Vs ===
21 Jan 2016There are two ways of comparing equality in Javascript.
- Lenient equality operator: “==” checks for value equality
- Strict equality operator: “===” checks for both value and type equality
var a = "42";
var b = Number( a ); // Explicit coercion
console.log(a == b); // true
console.log(a === b); // false
Best Practice: Whether to use == or ===
- If either value (aka side) in a comparison could be true, false, 0, “”, [] use ===
- Else it is safe to use ==