JS Booleans

JS Tutorial

JS Version

JS Objects

JS Function

JS Classes

JS Async

JS HTML DOM

JS Browser BOM

JS Web API

JS AJAX

JS JSON

JS vs JQUERY

JS Graphics

JavaScript Booleans

A JavaScript Boolean consists of one of two values: true or false.

Boolean Values

The data type that can only have one of two values, like

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

JavaScript has a Boolean data type. It can only take the values as true or false.

The Boolean() Function

Using the Boolean() function to find out if an expression (or a variable) is true:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Display the value of Boolean(10 > 9):</p>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML = Boolean(10 > 9);

</script>

</body>

</html>

Output

JavaScript Booleans

Display the value of Boolean(10 > 9):

Comparisons and Conditions

Here are some 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

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p id=”demo”></p>

<script>

document.getElementById(“demo”).innerHTML =

“100 is ” + Boolean(100) + “<br>” +

“3.14 is ” + Boolean(3.14) + “<br>” +

“-15 is ” + Boolean(-15) + “<br>” +

“Any (not empty) string is ” + Boolean(“Hello”) + “<br>” +

“Even the string ‘false’ is ” + Boolean(‘false’) + “<br>” +

“Any expression (except zero) is ” + Boolean(1 + 7 + 3.14);

</script>

</body>

</html>

Output

JavaScript Booleans

Everything Without a “Value” is False

The Boolean value of 0 (zero) is false:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Display the Boolean value of 0:</p>

<p id=”demo”></p>

<script>

let x = 0;

document.getElementById(“demo”).innerHTML = Boolean(x);

</script>

</body>

</html>

Output

JavaScript Booleans

Display the Boolean value of 0:

false

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

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Display the Boolean value of  -0:</p>

<p id=”demo”></p>

<script>

let x = -0;

document.getElementById(“demo”).innerHTML = Boolean(x);

</script>

</body>

</html>

Output

JavaScript Booleans

Display the Boolean value of -0:

false

The Boolean value of “” (empty string) is false

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Display the Boolean value of “”:</p>

<p id=”demo”></p>

<script>

let x = “”;

document.getElementById(“demo”).innerHTML = Boolean(“”);

</script>

</body>

</html>

Output

JavaScript Booleans

Display the Boolean value of "":

false

JavaScript Booleans as Objects

Normally JavaScript booleans are primitive values made from literals:

let x = false;

But booleans can also be specified as objects with the keyword new:

let y = new Boolean(false);

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Booleans and Boolean objects cannot be safely compared:</p>

<p id=”demo”></p>

<script>

// x is a boolean

let x = false;

// y is an object

let y = new Boolean(false);

document.getElementById(“demo”).innerHTML = typeof x + “<br>” + typeof y;

</script>

</body>

</html>

Output

JavaScript Booleans

Booleans and Boolean objects cannot be safely compared:

boolean

object

When using the == operator, x and y are equal:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Booleans and Boolean objects cannot be safely compared:</p>

<p id=”demo”></p>

<script>

let x = false;         // x is a boolean

let y = new Boolean(false);  // y is an object

document.getElementById(“demo”).innerHTML = (x==y);

</script>

</body>

</html>

Output

JavaScript Booleans

Booleans and Boolean objects cannot be safely compared:

true

When using the === operator, x and y are not equal:

Example

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Booleans</h2>

<p>Booleans and Boolean objects cannot be safely compared:</p>

<p id=”demo”></p>

<script>

let x = false;         // x is a Boolean

let y = new Boolean(false);  // y is an object

document.getElementById(“demo”).innerHTML = (x===y);

</script>

</body>

</html>

Output

JavaScript Booleans

Booleans and Boolean objects cannot be safely compared:

false