in Js

Difference between == and === in javascript

In JavaScript, == equals is called as equality with type coercion and === equals is called as equality without type coercion. Now, you must be thinking what exactly is type coercion ? At ease , we’ll talk about type coercion including our topic about difference between == and === equality signs.

In a simple term, Type coercion is all about converting a value from one type to another.

For an instance, if we do :

We are declaring two variable , one of type Number and another type of String. String can be defined with single quote or double quote. You can use typeof() function to know the type.

Now can you figure it out, what will happen if we do something like this :

This will return , true. But does that make any sense , we are comparing between two different types one of them is string and another one is number. This is the main part where Type coercion plays important role. Type coercion dynamically tries to figure out ‘1’ as declared as r  : which is string type so , all it does is String(s), which will return 1 as ‘1’ and compare them. Now they both are string so the result will be true.

Now, about === equality sign ? If we check the same compare method with === sign, this will return false. Why ? because === means equality without type coercion. This equality sign will not coerced any thing, it will strictly compare between those  variable.

Happy coding 🙂