DEV Community

Cover image for #3) Difference between "==" and "===" operators❔
Mayank Yadav
Mayank Yadav

Posted on • Edited on

#3) Difference between "==" and "===" operators❔

==(Equality)

✔Used for comparing two variables and ignoring the data type of variable.

✔Returns true only if the two operands are equal and false for not equal.

===(Strict Equality)

✔Used for comparing two variables but also checks the data type and compares the two values.

✔Returns true only if both the values and data types are same.


image


        🚀Both("==" and "===") are comparison operators.
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

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

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😅.

Collapse
 
232650413 profile image
孙志锋

这....