In JavaScript we have two type of equality comparison operators.
Non-strict and Strict
The difference between them is a very common question in
javascript interview. This blog will help you to review all the cases and understand the distinction.
- Non-strict (==) compare the values after converting the operands value to numbers
"1" == 1 // true
In the above example "1" on the left side will be converted to
number 1 .Hence, The Result will be true .
Different Cases :
" " == false // true
In programming terms false is 0 and true is 1.
" " and false will be converted to 0 which result into True.0 == false // true
false will be converted to 02 <= "4" // true
-
null == undefined // true
This case is pretty different from other case . If we convert
"undefined" to number the result will be NaN whereas in null
case it will be converted to 0.So how this
NaN == 0
can be result into true ?
"Null" and "undefined" are empty values and its javascript
core behaviour which result this into true. NaN == NaN
NaN is not equal to anything, including NaN
- Strict(===) compare the value without convertions, which is
quite useful to overcome cases like
0 == false
" 1 " === 1 // false
In the Above example the strict operator (==) does not convert
the operand type to number. Thus , the output is false.
Different Cases :
-
Two strings are strictly equal when they have the same
sequence of characters, same length, and same characters in
corresponding positions."abc" === "abc" // true
"abc" === "abcc" // false
"abc" === "acb" // false
Two Boolean operands are strictly equal if both are true or
both are false.
true === true //true
true === 1 //false
Two objects are strictly equal if they refer to the same
Object.
{} === {} //false
Null and Undefined types are not equal with ===, but equal
with ==.
null == undefined // true
null === undefined // false
Thanks for your read 🙌
Top comments (0)