We all know that JavaScript is quite a funny language with tricky parts.
Please, explain how it works, if possible: [ ]
is equal ![ ]
.
[ ] == ![ ] // -> true
I don't understand it, why the result is "true"
?
Because ==
doesn't compare them typesafe, but the values get casted:
[] == ![]
//is
[] == false
//is casted into
"" == false
//and that into
false == false
//wich is
true
use the typesafe comparison
console.log(`[] == ![]`, [] == ![])
console.log(`[] === ![]`, [] === ![])
in my opinion, the only reason to use ==
is when you chack against null
or undefined
and want to cover both. So, writing
value == null
//or
value != null
//instead of the more explicit (but longer)
value === null || value === undefined
//respectively
value !== null && value !== undefined
everywhere else I'd use ===
because of these funny results that ==
sometimes has.
Here's a funny little clip on that topic, enjoy: https://www.destroyallsoftware.com/talks/wat