One liner to parse a JavaScript string into a boolean

This ES6 one liner is a handy little JavaScript method for converting a string value into a boolean:

 

This maps values such as ‘true’, ‘TRUE’ ‘on’, ‘1’, ‘-1’ to true, whilst values such as undefined, null, ‘false’, ‘FALSE’, ‘0’, ‘off’ are treated as false.

View Demo

Here’s a TypeScript version of the same thing:-

 

and just for completeness, below is the ES5 version

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.