Difference between '==' and '===' in JavaScript.
what is '==' ?
Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison. In the example given below, '1' is a string but as we used == it converts the string to a number and returns true.
1 == '1'// true
'' == '0' // false
0 == '' // true
0 == '0' // true
false == undefined // false
false == null // false
null == undefined // true
what is '==='?
===
is used for comparing two variables, but this operator also checks the datatype and compares two values. It is more strict than ==.
1 === '1'// false
1 === 1 //true
key differences
==
Checks the equality of two operands without considering their type whereas===
checks the equality along with their datatype.==
checks for equality only after doing the necessary conversion whereas===
directly takes the type and compares it without any conversions.
Conclusions:
==
means: when we compare a number to string it, like 1 = '1' it will tell the string '1' that you are a number and return true.===
means: it is just a strict form of the operator where it considers the value as well as the data type.