JavaScript has a built-in Boolean()
function that you can use to turn any value into its boolean equivalent.
For example, the following string value z
will be converted to true
using the Boolean()
function:
let bool = Boolean('z');
console.log(bool); // true
console.log(typeof bool); // "boolean"
Passing a truthy value to the function will return true
while passing falsy values will return false
:
let boolNull = Boolean(null);
console.log(boolNull); // false
console.log(typeof boolNull); // "boolean"
See also: JavaScript truthy falsy values explained