Did you know ===
also coerces type in JavaScript?
However, unlike ==
, ===
will always return false
if the types differ.
That ==
and ===
both perform type coercion under the hood in JavaScript is such a fun fact!
Thanks for stopping by and sharing your knowledge with us Clyde Grey!!!
I have written previous about the difference between loose equality and strict equality in JavaScript, but I never realized that ===
includes a type coercion as well.
It’s right there in the ECMAScript Specification:
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
Indeed there is a type comparison with ===
, and if the types differ the result is always false
.
But I had never really thought about it that way!
Of course, it’s just a bit of trivia, and the idea can be a bit confusing for those new to JavaScript, since ==
compares values after type coercion but ===
will never return true
for values of different types.
Thanks again for sharing!