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

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

hat == 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:

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!

Image for post
Image for post
Photo by Erol Ahmed on Unsplash

Written by

🤓 The physical therapist who writes JavaScript 💪 Web Developer 😎 Mentor 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 🗯️ Ask me anything 👉 DoctorDerek.com 👈

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store