The difference is that if used ==
there will be a coercion of the value for both sides of the expression to have the same type.
In the case of ===
there will be no coercion and so the code below will be false:
if(1 === '1')
console.log('igual');
else
console.log('diferente');//Esta será a resposta
Already if we use only ==
there will be a coercion for both to be the same type and will give equal.
if(1 == '1')
console.log('igual');//Esta será a resposta
else
console.log('diferente');
Another example:
if(0 == '')
console.log('igual');//Esta será a resposta
else
console.log('diferente');
In the case ''
is considered a value falsey
, which can be considered false even if it does not have the value false
.
In the case below is given the correct message, which are different:
if(0 === '')
console.log('igual');
else
console.log('diferente');//Esta será a resposta
Edited
Here are some other values that may result in strange cases in the logical comparison in javascript
:
False report:
0
''
' '
null
undefined
NaN
The question is different, because the OP is asking because in one case returns false and other true, but not focused on the real meaning of operators, which is what I wish to know.
– Paulo Roberto Rosa
@Pauloroberto The answer is the same, I consider yes duplicate.
– bfavaretto
Okay, you can consider a duplicate, but the question is different, and the answer I need is not found in the answers in your "duplicate"
– Paulo Roberto Rosa
http://meta.pt.stackoverflow.com/questions/607/lidando-com-perguntas-duplicadas
– Maniero
@Bigown ok, so this is a similar duplicate, different from the exact one, so the question should remain open.
– Paulo Roberto Rosa
It seems that most disagree. No one is even considering reopening.
– Maniero
I edited the other question to get a little more generic. Why do you think the answers there do not meet you?
– bfavaretto