2021年2月9日星期二

Why does javascript if statement return true when string/numeric(nonzero) inputs are given as it's condition?

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.  
https://stackoverflow.com/questions/66131047/why-does-javascript-if-statement-return-true-when-string-numericnonzero-inputs February 10, 2021 at 12:47PM

没有评论:

发表评论