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.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s