Black Friday Sale is on: Get Amazing deals on tools and resourcess

Tutorial: Javascript Boolean


By Emmanuel Chinonso

Web Developer

JavaScript Booleans

True or false are the two values that a JavaScript Boolean represents.

Contrast Bootstrap UI Kit

Boolean Values

In programming, you'll frequently require a data type that can only have one of two values, like

  • YES / NO
  • ON / OFF

JavaScript offers a Boolean data type for this. It can only take the true or false values.

The Boolean() Function: To determine whether an or a variable is true, use the Boolean() function:

JavaScript Code:

1Boolean(10 > 9); // returns true

Or, to make things more simpler:

JavaScript Code:

110 > 9; // also returns true
210 > 9; // also returns true

Comparisons and Conditions

Comparison Operators: In logical expressions, comparison operators are used to assess if variables or values are equal or different. The table below explains the comparison operators given x = 5:

==equal tox == 8false
==equal tox == "5"true
==equal tox == "5"true
===equal value and equal typex === 5true
===equal value and equal typex === "5"false
!=not equalx != 8true
!==not equal value or not equal typex !== 5false
!==not equal value or not equal typex !== "5"true
!==not equal value or not equal typex !== 8true
>greater thanx > 8false
<less thanx < 8true
>=greater than or equal tox >= 8false
<=less than or equal tox <= 8true

Conditional statements are covered in detail in the chapter JS Conditions.

Here are some examples:

OperatorDescriptionJavaScript Code
==equal toif (day == "Monday")
>greater thanif (salary > 9000)
<less thanif (age < 18)

All JavaScript comparisons and conditions are based on the Boolean value of an expression.

How Can it be Used

In conditional statements, comparison operators can be used to compare values and perform action based on the outcome.: if (age < 18) text = "Too young to buy alcohol";

Logical Operators The logic between variables or values is determined using logical operators. The table below explains the logical operators given x = 6 and y = 3:

OperatorDescriptionExample Try it
&&and(x < 10 && y > 1) is true
IIor(x == 5
!nor!(x == y) is true

Conditional (Ternary) Operator: A conditional operator in JavaScript assigns a value to a variable based on a set of criteria.

Syntax variablename = (condition) ? value1:value2

JavaScript Code:

1var voteable = age < 18 ? 'Too young' : 'Old enough';

The value of voteable will be "Too young" if the variable age is less than 18, and "Old enough" if the variable age is greater than 18.

Comparing Different Types

When multiple types of data are compared, surprising results can occur. When comparing a string to a number, JavaScript converts the string to a number before comparing the two. The value of an empty string is 0. When a string is not numeric, it translates to NaN, which is always false.

2 < 12true
2 < "12"true
2 < "John"false
2 > "John"false
2 == "John"false
"2" < "12"false
"2" > "12"true
"2" == "12"false

Because 1 is less than 2 (alphabetically), "2" will be greater than "12" when comparing two strings. Variables should be changed to the correct type before comparison to provide a good result:

1age = Number(age);
2if (isNaN(age)) {
3 voteable = 'Input is not a number';
4} else {
5 voteable = age < 18 ? 'Too young' : 'Old enough';

Contrast Bootstrap UI Kit

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro


Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts