Difference between == (double equal to) and === (triple equal to) in Javascript.

Difference between == (double equal to) and === (triple equal to) in Javascript.

by Himanshu Kesarwani

So, Hello all Awesome JavaScript developers!

JavaScript is one of the most popular Languages currently in the world. It has numerous applications from Front End to Backend. Even different frameworks and libraries are built on top of it.

But when we are learning JavaScript initially, we know single equal to means the assignment operator but we all come across the dreaded == and === equal to signs, and often get confused about what to use and when. Both of them are comparison operators but have slightly different applications.

In this blog, we will deep dive into it and understand both of the comparison operators with proper examples.

Double Equal to ( == ) comparison operator.

Double equal is a loose or abstract equality operator meaning that it will only check whether the values are the same or not but not the type. It will convert both of them to the same type automatically and return true if they match.

console.log( 1 == "1")
// true

console.log( 0 == true)
// false

Triple Equal to ( === ) comparison operator.

Triple Equal to operator is also known as equal value and equal type operator. It is a strict equality operator as it checks for both value and the type of the value.

If the type of values are not same, it will return false. For example, if we are comparing string "2" with the number 2, it will return false as the values are not the same.

console.log("2" === 2);
// false

Which one is better?

Both of them are useful and it truly depends on the developer to choose from one of them depending on the need. As we have seen above, if we want to check both value and type we can the latter one (===). One prime advantage of using the "===" operator is that it avoids unexpected behavior that can occur when using the "==" operator.

Conclusion

In conclusion, both == and === are useful when comparing values in JavaScript, but it's really important to understand the differences. While the "==" operator can be useful in certain situations, the "===" operator is generally a safer choice and can help avoid any unexpected behavior.

So that's it for this blog, hope I was able to help you understand it better, Keep Learning, One step at a time, you are doing just awesome!

Open to feedback, constructive criticism, and of course suggestions!