JavaScript Boolean() function explained
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
More JavaScript tutorials
See allJavaScript
Sort Keys in Map Using JavaScript
Dec 11, 2023
JavaScript
Get Element by Data Attribute Using JavaScript
Dec 8, 2023
JavaScript
Convert Milliseconds to Hours Minutes Seconds in JavaScript
Dec 6, 2023
JavaScript
JavaScript Get the Index of Max Value in Array
Nov 16, 2023
JavaScript
JavaScript Check if Value is an Object
Nov 15, 2023