DEV Community

Discussion on: #3) Difference between "==" and "===" operators❔

Collapse
lexlohr profile image
Alex Lohr

It's a bit more complex than that. The weak typed comparison == performs type coercion if necessary before the actual comparison. This means everything will be coerced into the type of the simplest value of both sides, e.g.

{} == '[object Object]' // true
Enter fullscreen mode Exit fullscreen mode

The simplest type here is the string on the right, so the .toString() method is applied before the comparison. The order of complexity from top to bottom is object (includes arrays and other instances), function, string, number, boolean. null is actually an object and undefined contains no value at all and thus is coerced to false.

So the weak typed comparison has to do a lot more than the type-safe one and thus is slower.

Collapse
myk profile image
Mayank Yadav Author

Thanks for the comment, I really appreciate that.❤
My main focus was highlighting the basics of these operators so that for beginners it's easy to understand otherwise most of then would run away😅.