0

I have a question about type conversions. Given the following table from w3schools.com...

table of conversions

Why are the strings "0" and "000" converted to boolean true?

user229044
  • 232,980
  • 40
  • 330
  • 338
Norayr Ghukasyan
  • 93
  • 1
  • 1
  • 6

2 Answers2

1

Because when you coerce a value to boolean in JavaScript, any non-blank string is true. Only blank strings are false.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks very much ... its already clear for me ... Just one ... I cant understand why people in stackoverflow minuses my question (-) ... yes may be this is a duplicate , but i am a beginner and i couldnt find the answer ... – Norayr Ghukasyan Aug 02 '17 at 14:12
  • @NorayrGhukasyan: I can't say, I didn't downvote your question. But the most likely reason would be the first part of what it says on the downvote button: "...does not show any research effort..." The rules for JavaScript type coercion are **extremely** well-covered in lots of places on the web. – T.J. Crowder Aug 02 '17 at 14:14
0

The reason of it is because both "0" and "000" are strings and not numbers.

Any string which is non-empty converted to boolean is going to be true.

RottenCheese
  • 869
  • 2
  • 12
  • 18
  • Thanks very much ... its already clear for me ... Just one ... I cant understand why people in stackoverflow minuses my question (-) ... yes may be this is a duplicate , but i am a beginner and i couldnt find the answer ... – Norayr Ghukasyan Aug 02 '17 at 14:12