Boolean()
Converts a value to true or false.
Available since: ES1
Syntax
Boolean(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to convert to a boolean. |
Return value
booleantrue or false.
Examples
Falsy values:
console.log(Boolean(0), Boolean(''), Boolean(null), Boolean(undefined), Boolean(NaN));Output:
false false false false falseEverything else is truthy, even empty arrays and the string '0':
console.log(Boolean('0'), Boolean([]), Boolean({}), Boolean(-1));Output:
true true true trueRemoving falsy values from an array:
console.log(['Anna', '', null, 'Oleg', 0].filter(Boolean).join(', '));Output:
Anna, OlegNotes
There are only eight falsy values: false, 0, -0, 0n, '', null, undefined and NaN. The short form is double negation, !!value.
See also
- Number() — Converts a value to a number.
- String() — Converts any value to a string.
- Logical OR (||) — Logical OR: returns the first truthy value.
Related recipes
In the tutorial
- Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.