Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Boolean()

Converts a value to true or false.

Available since: ES1

Syntax

Boolean(value)

Parameters

ParameterTypeDescription
valueanyThe 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 false

Everything else is truthy, even empty arrays and the string '0':

console.log(Boolean('0'), Boolean([]), Boolean({}), Boolean(-1));

Output:

true true true true

Removing falsy values from an array:

console.log(['Anna', '', null, 'Oleg', 0].filter(Boolean).join(', '));

Output:

Anna, Oleg

Notes

There are only eight falsy values: false, 0, -0, 0n, '', null, undefined and NaN. The short form is double negation, !!value.

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.