skip to content
Alvin Lucillo

Boolean gotcha in JS

/ 1 min read

💻 Tech

If you want to check if a value is truthy or falsy, you can use the Boolean function. However, one common mistake is to use that to convert a string into a Boolean value. In the example below, a non-empty and non-null value will always result to true no matter the value, even if the value is “false”.

console.log(Boolean("false"));    // true
console.log(Boolean("0"));        // true
console.log(Boolean("no"));       // true
console.log(Boolean(""));         // false
console.log(Boolean(null));       // false
console.log(Boolean(undefined));  // false