JavaScript Boolean Primitive
A boolean primitive represents true or false.
The type name is boolean.
Boolean values are one of JavaScript's 7 primitive types.
Creating Boolean Values
A boolean value can be either true or false.
Type Information
The type of a primitive boolean value is "boolean".
Boolean Expressions
Boolean values are often the result of comparisons.
| Expression | Result |
|---|---|
10 > 5 |
true |
10 < 5 |
false |
10 == 10 |
true |
10 != 10 |
false |
Boolean Conversion
The Boolean() function converts values to boolean values.
| Expression | Result |
|---|---|
Boolean(1) |
true |
Boolean(0) |
false |
Boolean("Hello") |
true |
Boolean("") |
false |
Boolean(null) |
false |
Boolean(undefined) |
false |
Boolean(NaN) |
false |
Truthy and Falsy Values
Every JavaScript value has an associated boolean value.
Values that convert to true are called truthy.
Values that convert to false are called falsy.
Falsy Values
JavaScript has only a small number of falsy values.
| Value |
|---|
false |
0 |
-0 |
0n |
"" |
null |
undefined |
NaN |
All other values are truthy.
Using Booleans in Conditions
Boolean values are commonly used in conditional statements.
Primitive Booleans vs Boolean Objects
The new Boolean() constructor creates a Boolean object.
Boolean literals create primitive boolean values.
| Primitive - Recommended | Boolean Object - Rarely needed |
|---|---|
let x = true |
let x = new Boolean(true) |
typeof x === "boolean" |
typeof x === "object" |
Boolean Objects Are Truthy
All objects are truthy.
This includes Boolean objects that contain false.
Use primitive boolean values instead of Boolean objects.