Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Booleans

Boolean Values

In programming, you frequently require a data type that can have one of two values, such as

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this purpose, JavaScript provides the Boolean data type, which can only be true or false.

The Boolean() Function

You can use the Boolean() function to determine whether an expression (or variable) evaluates to true.

Example

Boolean(10 > 9)

Or even more simply:

Example

(10 > 9)
10 > 9

Comparisons and Conditions

The chapter on JS Comparisons provides a comprehensive overview of comparison operators.

The chapter on JS If Else offers an in-depth look at conditional statements.

Here are a few examples:

Operator

Description

Example

==

equal to

if (day == “Monday”)

greater than

if (salary > 9000)

less than

if (age < 18)

Everything With a “Value” is True

Examples

100

3.14

15

“Hello”

“false”

7 + 1 + 3.14

Everything Without a “Value” is False

The Boolean value of 0 (zero) is considered false.

let x = 0;
Boolean(x);

The Boolean value of -0 (minus zero) is also considered false.

let x = –0;
Boolean(x);

The Boolean value of an empty string (“”) is evaluated as false.

let x = “”;
Boolean(x);

The Boolean value of undefined is regarded as false.

let x;
Boolean(x);

The Boolean value of null is considered false.

let x = null;
Boolean(x);

The Boolean value of false is, as expected, false.

let x = false;
Boolean(x);

The Boolean value of NaN is false:

let x = 10 / “Hallo”;
Boolean(x);