Today I've come across a problem in Javascript that I can't understand at all.
I have this code:
var status = false;
console.log(status, typeof status);
if (status) {
console.log("status is true");
} else {
console.log("status is false");
}
I expected it would not satisfy the if
statement, since status
is false
. Moreover, it's a Boolean, not a String.
I was wrong. It returned "status is true"
, so status
did satisfy the if
statement.
I changed my code to specify a condition, not just the variable:
var status = false;
console.log(status, typeof status);
if (status == false) {
console.log("status is false");
} else {
console.log("status is not false");
}
Notice that I switched the output messages, since we're not checking the value of the variable itself, but checking if it's false
or not.
I really started to panic when I read "status is not false"
on the console. How can it be other thing than false
?
What am I missing?