JS Value to Boolean
Using Boolean() Function
const value = "Hello World"; const booleanValue = Boolean(value); console.log(booleanValue); // true
The Boolean() function converts the value to a boolean and returns it. The above example will return true as
"Hello World" is a non-empty string and thus, a truthy value.
Using Double NOT(!!) Operator
const value = "Hello World"; const booleanValue = !!value; console.log(booleanValue); // true
The double NOT(!!) operator converts the value to a boolean as well, but in a more concise way. The above example will return true for the same reason as before.
- "" (empty string)
Any other value not listed above is considered a truthy value. So, be careful when using boolean conversions in your code and always test it thoroughly.