Consider following JavaScript code (tested in Firefox):
function f(a) {
if (a == undefined) {
alert('undefined');
}
if (a == null) {
alert('null');
}
}
f();
Both alerts are shown, suggesting that both statements are true.
Could you provide a reasonable explanation?
==is a “soft” equality operator.It uses type coercion to compare two equivalent objects as equal.
All of the following are true:
Instead, you should use the
===operator, which checks for strict equality.