Difference between == and === in Javascript

While conducting interview for a Front end developer perspective, It is essential to know the difference between == and === comparisons operators in Javascript.

Javascript supports type converting comparisons and strict comparisons. Difference between both is as follows:


I > ‘==’ is known as “equal to” and ‘===’ is known as “equal value and equal type”

II > When we compare two variables of different type i.e. a number with a string or String with a Boolean using == operator, it automatically converts one type into another and return value based upon content equality, while === operator is strict equality operator, only return true if both variable of same type and also contains same value.

To understand this scenario follow the example:

"2017" == 2017    // returns true using auto type converter, Number converted into string
"2017" === 2017   //  returns false as both variables are not of the same type

1 == true         // returns true, As 1 is equivalent true 
1 === true        // returns false

 

It’s best practice to always use ‘===’ when you are comparing two objects or variables.

 

Join Discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.