Difference equals operator (== vs ===) in JavaScript and which to use when?


I came up with two javascript comparison operators ==(two equals) and ===(three equals) sign, I would like to know when I should use which of these comparison operators and when?

Is there any performance improvement when using ===? 

Thanks

 


Asked by:- bhanu
1
: 77 At:- 9/6/2017 11:26:14 AM
javascript operatos identity-operator equality-operator






1 Answers
profileImage Answered by:- pika

In javascript, 3 equal signs(===) means "equality without type coercion". Using the triple equals, the values must be equal in type as well, to better understand it, check this comparison below

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false again for same reason, type is not same
'0' == false // true because basically in logical operator fasle emans 0
'0' === false // false becasue type is different

From the above answer you can understand, The == operator will compare for equality by doing any necessary type conversions(if required) while on the other hand === operator , will not do the conversion, so if two values are not the same type === will simply return false.

So you can say === and !== are strict comparison operators in javascript, check this URL from Mozilla.Org

In terms of performance, === is not quicker if the types are the same. If types are not the same, === will be quicker because it won't try to do the conversion

1
At:- 9/7/2017 7:33:46 AM
great answer with an example, as needed, thanks 0
By : bhanu - at :- 9/11/2017 1:39:13 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use