In Javascript there are two pairs of equal operators: === and !==, and the evil twins (evil Twins) == and != (as described in Javascript The Good Parts by Douglas Crockford).
=== and !==
The first pair of operators, === and !==, works like the == and !== in most programming languages. So if the values compared with === have the same value and are of the same type, the expression will result in true, already the !== indicates the opposite to that statement.
Examples using === and !==
2 === 2 // true
'ola' === 'ola' // true
'' === '0' // false
0 === '' // false
0 === '0' // false
false === 'false' // false
false === '0' // false
false === undefined // false
false === null // false
null === undefined // false
' \t\r\n ' === 0 // false
== and !=
The second pair of operators, == and !=, work as follows. When both compared values are of the same type, the evil twins behave like the other pair of operators (=== and !==), but when the values compared are of different types, they try to correct the values through a conversion before making the comparison. This sounds cool, but can generate hard-to-understand results and makes it difficult to maintain code.
Examples using == and !=
2 == 2 // true
'ola' == 'ola' // true
'' == '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
A recommendation given by Douglas Crockford is never to use evil twins, in their place always use === and !==.
Note that an elegant way (in my view) of writing code like this is
if (!x) { /* acessa */ }– Camilo Martin
@Camilomartin Yes, but
!xgives true for some values thatx === falsewould false, asx = 0.– luiscubal