Did you know === also coerces type in JavaScript?

However, unlike ==, === will always return false if the types differ.

Dr. Derek Austin 🥳
1 min readJan 7, 2020

--

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 !!!

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:

If Type(x) is different from Type(y), return false.

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!

Photo by Erol Ahmed on Unsplash

--

--

Dr. Derek Austin 🥳
Dr. Derek Austin 🥳

Written by Dr. Derek Austin 🥳

Hi, I'm Doctor Derek! I've been a professional web developer since 2005, and I love writing about programming with JavaScript, TypeScript, React, Next.js & Git.

No responses yet