0
if("0") - why is it a true?
Hi everyone, I would like to ask why this statement above its true? I tried it with "1" and "2" and this was true too, but I know that there is conversion to number and any number (expect 0) == true. So why isn't it a false? //Edit: If there is a conversion, then why "145" == true will be false? I'm very confused right now :/
5 Antworten
+ 3
@NoName wrote: "So strings in if statement are always been converted to number 1?"
Not at all.
String aren't converted, they are evaluate as boolean value. And in JS, all non empty strings are evaluted to True, and False in the other case...
if ("") alert("true"); else alert("false"); // output: "false"
if ("0") alert("true"); else alert("false"); // output: "true"
... but:
if ("0"==true) alert("true"); else alert("false"); // output: "false"
... as:
if ("0"==1) alert("true"); else alert("false"); // output: "false"
0
("0") is string and 1 and true
0
So strings in if statement are always been converted to number 1?
0
yes
0
Okay, thanks a lot :)