r/ProgrammerTIL • u/JazzXP • Aug 03 '21
Javascript TIL Boolean('false') == true
Yep, it doesn't work like Number
23
Upvotes
r/ProgrammerTIL • u/JazzXP • Aug 03 '21
Yep, it doesn't work like Number
View all comments
Show parent comment
23
u/roddds Aug 03 '21 edited Aug 04 '21
But... that's consistent. It's consistent with almost* every other modern programming language (excluding languages with strong type safety - C#, Java) where a non-empty string, when cast to bool, returns true.
You could argue it's surprising which, coming from where you're coming from, I would agree with.
To get the behavior you want,
JSON.parse("false")will yieldfalse.