Nikita Kazakov
Nikita Kazakov
~1 min read

Tags

Often I’ll have a boolean value passed in as a string. This means an if statement will have to look like this:

proceed = 'true'

if (proceed === 'true') { 
    // some code here.
}

You’re probably thinking, why not coerce it.

Boolean('true')  //=> true
Boolean('false') //=> true!??!?!?

That won’t work as a false string is also coerced to true.

Instead, do this and make your code prettier and easier to understand.

proceedIsTrue = proceed === 'true'

if (proceedIsTrue) { 
    // Code runs if true
}