Junior
What is the difference between == and === in JavaScript?
sobes.tech AI
Answer from AI
The == operator (loose equality) performs type coercion of operands before comparison.
The === operator (strict equality) does not perform type coercion. It compares both the values and the types of operands.
// Examples of ==
console.log(5 == '5'); // true (the string '5' is coerced to the number 5)
console.log(true == 1); // true (the number 1 is coerced to true)
console.log(null == undefined); // true (special rule)
// Examples of ===
console.log(5 === '5'); // false (number type is not equal to string type)
console.log(true === 1); // false (boolean type is not equal to number type)
console.log(null === undefined); // false (null type is not equal to undefined type)
Using === is preferable in most cases because it is more predictable and avoids the implicit behavior associated with type coercion.