What are the differences between == and === in JavaScript?
|Comparing is very common in any programming language. If i say in simple words, == will not check types and === will check type and value both.
if(a == 10) { }
When comparing is using (== ) then it will check the value of variable and convert it into common type and return true if both are equals. Comparing number with string also return true if both have same value.
For example, if we compare 2 strings then they should have same characters set . Numbers must have same value.
As Douglas Crockford’s stated :
JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable.These are some of the interesting cases:
'' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true