1

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?

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Ender
  • 13
  • 3

1 Answers1

-1

You are experiencing this because status is a reserved word in js. Use another variable name like stat and it will work just fine.

For more info

var stat = false;
if(stat){
    console.log("status is true");
} else {
    console.log("status is false");
}
ellipsis
  • 12,049
  • 2
  • 17
  • 33