TIL Loose equality
POSTED ON:
TAGS: javascript wat
Loose Equality (double equals) is the bogeyman of JavaScript.
Here’s just a couple of examples to make your skin crawl:
console.log([[]] == ''); // true
console.log(true == [1]); // true
console.log(false == [0]); // true
When you use == #
When you use === #
Additional: #
Overall, there's no real reason to ever user ==.
But there is one usage of it that is relatively common and is worth knowing:
if (x == null) {
// ...
}
This code is equivalent to writing:
if (x === null || x === undefined) {
// ...
}
Related TILs
Tagged: javascript