Skip to main content

Boolean

Properties​

Definition​

A Boolean is a logical value: true or false. It answers questions like "Is this statement correct?"

> typeof true
"boolean"

> typeof false
"boolean"

Using booleans​

Booleans can be created using literals or the Boolean constructor:

// Literal syntax
> true
true

> false
false

// Boolean constructor
> new Boolean(true)
[Boolean: true]

> Boolean(true)
true

Truthy and falsy values​

In JavaScript, values are automatically converted to booleans in boolean contexts. Some values are "truthy" (convert to true) and others are "falsy" (convert to false).

Falsy Values​

These values evaluate to false:

ValueTypeNotes
falsebooleanThe literal false
0numberZero
-0numberNegative zero
0nbigintBigInt zero
""stringEmpty string
nullnullRepresents "no value"
undefinedundefinedRepresents "not defined"
NaNnumberNot-a-Number

Truthy Values​

Everything else is truthy, including:

ValueTypeNotes
truebooleanThe literal true
1numberAny non-zero number
-1numberNegative numbers
"hello"stringAny non-empty string
"0"stringString "0" (string, not number)
"false"stringString "false" (string, not boolean)
[]arrayEmpty array
{}objectEmpty object
function() {}functionFunctions

Operations on booleans​

Logical operators: Truth tables​

The && operator​

Returns the first falsy value, or the last value if all are truthy.

ExpressionResultExplanation
true && truetrueBoth values are true
true && falsefalseOne is false
false && truefalseOne is false
false && falsefalseBoth are false

When used with non-boolean values, && returns the first falsy value or the last truthy value:

ExpressionResultExplanation
"hello" && "world""world"Returns last truthy
"" && "world"""Returns first falsy

The || operator​

Returns the first truthy value, or the last value if all are falsy.

ExpressionResultExplanation
true || truetrueAt least one is true
true || falsetrueFirst value is true
false || truetrueSecond value is true
false || falsefalseBoth are false

When used with non-boolean values, || returns the first truthy value or the last falsy value:

ExpressionResultExplanation
"hello" || "world""hello"Returns first truthy
"" || "world""world"Returns first truthy

The ! operator​

Returns the opposite boolean value.

ExpressionResultExplanation
!truefalseNegates the value
!falsetrueNegates the value

When used with non-boolean values, ! converts to boolean first, then negates:

ExpressionResultExplanation
!"hello"falseString is truthy, negated to false
!""trueEmpty string is falsy, negated to true
!0trueZero is falsy, negated to true
!1falseOne is truthy, negated to false

Double NOT (!!)​

Converts a value to a boolean by negating twice.

ExpressionResultExplanation
!!"hello"trueDouble negation converts to boolean
!!""falseDouble negation converts to boolean
!!0falseDouble negation converts to boolean
!!1trueDouble negation converts to boolean

Comparison operations​

Comparison operations return boolean values:

// Equality
> 5 == 8
false

> 5 === 8
false

// Inequality
> 5 != 8
true

> 5 !== 8
true

// Greater than
> 5 > 8
false

// Less than
> 5 < 8
true

// Greater than or equal
> 5 >= 8
false

// Less than or equal
> 5 <= 8
true

Boolean conversion​

The Boolean constructor can convert values to booleans:

> Boolean(1)
true

> Boolean(0)
false

> Boolean("hello")
true

> Boolean("")
false

> Boolean(null)
false

> Boolean(undefined)
false

Boolean methods​

toString()​

Converts a boolean to a string.

> true.toString()
"true"

> false.toString()
"false"

valueOf()​

Returns the primitive value of a boolean object.

> const bool = new Boolean(true)
undefined

> bool.valueOf()
true