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 :/

18th Feb 2017, 6:08 PM
***
5 Réponses
+ 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"
19th Feb 2017, 7:48 AM
visph
visph - avatar
0
("0") is string and 1 and true
18th Feb 2017, 6:27 PM
afgprogrammer
afgprogrammer - avatar
0
So strings in if statement are always been converted to number 1?
18th Feb 2017, 6:29 PM
***
0
yes
18th Feb 2017, 6:29 PM
afgprogrammer
afgprogrammer - avatar
0
Okay, thanks a lot :)
18th Feb 2017, 6:30 PM
***