DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on

Problem with the string 'false'

Alt Text

Top comments (8)

Collapse
 
ben profile image
Ben Halpern

I'm always nervous about words like "false" no matter what the language situation is.

I'll find myself writing things like if string == "Definitely 100% true" so the only true value is that and everything else is false, just on paranoia that I don't understand the truthiness well enough.

Collapse
 
lionelrowe profile image
lionel-rowe

Depends on the context. If the setting is requirePasscodeBeforeLaunchingNukes, you might want to use !== "false" instead πŸ˜…

Collapse
 
ml318097 profile image
Mehul Lakhanpal

Haha... Nice one πŸ˜‚

Collapse
 
ml318097 profile image
Mehul Lakhanpal

Specially javascript. Difficult to understand truthy and falsy values πŸ˜…

Collapse
 
lionelrowe profile image
lionel-rowe • Edited

If you know value is either "true" or "false", you can also use JSON.parse(value).

JSON.parse('true') // true
JSON.parse('false') // false
JSON.parse('tru') // SyntaxError: Unexpected token t in JSON at position 0
JSON.parse('"string"') // "string"
Collapse
 
ml318097 profile image
Mehul Lakhanpal

Awesome. Dint know that. I actually faced the issue in query string. Dint realise that it was taking as string πŸ˜‚πŸ™ˆ

Collapse
 
ml318097 profile image
Mehul Lakhanpal

Angular js?

 
ml318097 profile image
Mehul Lakhanpal

πŸ‘πŸ‘