-2

I'm new to coding, and I wonder why the statement below gives true values.
Can someone give explanations for the logic?
When give numeric inputs, if the condition tests whether if it is a number, I think it should return true instead of false.
Thanks for helping out.

// Statement 1
if ("d") {
  reply = "TRUE!";
} else {
  reply = "FALSE?!";
}
console.log(reply)
// Returns "TRUE!"

// Statement 2
if (2) {
  reply = "TRUE!";
} else {
  reply = "FALSE?!";
}
console.log(reply)
// Also returns "TRUE!"

// Statement 3
if (0) {
  reply = "TRUE!";
} else {
  reply = "FALSE?!";
}
console.log(reply)
// Why does this return "FALSE?!"? I'm aware that 0 is false in boolean, but cannot understand how the syntax works considering statement 2.
June Park
  • 47
  • 3

2 Answers2

0

Inputs that are not 0, null, underfined, NaN, or empty string will be judged as true

红日初升
  • 141
  • 1
  • 7
0

Because 0 is the only number that equates to false. Other than that, statement two is equivalent to if(2==2)

alec wilson
  • 176
  • 1
  • 3
  • 13