JavaScript

# JavaScript Boolean Operators | Explained

Many foundations of mathematical logic are found in the field of computer science that be utilized to add logic related to Boolean algebra, truth tables, and comparisons in a program.

The JavaScript programming language offers different operators to evaluate expressions based on boolean variables. These operators help to maintain the programming flow control with respect to the added mathematical logic. More specifically, JavaScript Boolean operators allow you to perform different comparisons on the specified variables and evaluate the results.

This write-up will explain the working of the JavaScript Boolean operators. So, let’s start!

## JavaScript Boolean Operators

Here is the list of Boolean operators supported by JavaScript:

• Boolean OR ||” operator
• Boolean AND &&” operator
• Boolean NOT !” operator

We will now discuss the usage of each of the mentioned JavaScript Boolean operators in the following sections.

## How to use JavaScript Boolean OR operator

In JavaScript, the Boolean ORoperator is utilized to evaluate boolean variables’ values. It is represented using the double pipe operator “||”.

While using the Boolean “OR” operator, If the value of both of the specified variables is “true,” then the expression is considered as “truthy,” and it will return “true” value; otherwise, the return case of the OR operator will be set to “false”.

### Syntax of JavaScript Boolean OR operator

result = x || y

Here, the Boolean OR operator “||” will evaluate the expression “x || y” based on their values and store the returned value in the “result” variable.

## Truth table of JavaScript Boolean OR operator

The below-given truth table illustrates the OR operator result based on the specified values of “x” and “y” boolean variables:

 x y x || y true true true true false true false true true false false false

According to the truth table, if the value of any operand is “true”, the Boolean OR operator “||” will mark the expression as “true” in case both values are “false,” then the OR operator will also return a “false” value.

Now, let’s check out a practical example of implementing the functionality of the JavaScript Boolean OR operator.

## Example: How to use JavaScript Boolean OR operator

First of all, we will create two boolean variables named “x” and “y” having the following values:

let x = true,

y = false;

Next, we will use the Boolean OR operator “||” for evaluating the values of “x” and “y”:

console.log(x || y);

Execution of the provided code will return “true” because the value of one of the specified boolean variables is “true”:

In case, if the values of both “x” and “y” variable is “false”, then the expression “x || y” will return “false”:

let x = false,

y = false;

console.log(x || y);

Output

## How to use JavaScript Boolean AND operator

Like the OR operator, the JavaScript Boolean AND operator is also used to evaluate the values of added boolean variables. It is represented by using double ampersand &&” sign.

While utilizing JavaScript Boolean AND operator, if both values are “true,” the expression is considered “truthy.” In the other case, if either of one value is “false“, the return case of AND operator will be set to “false”.

### Syntax of JavaScript Boolean AND operator

result = x && y

Here, the Boolean AND operator “&&” will evaluate the expression “x && y” based on their values and store the returned value in the “result” variable.

## Truth table of JavaScript Boolean OR operator

Now, check out the following truth table of the JavaScript Boolean AND operator to understand how it works:

 x y x && y true true true true false false false true false false false false

According to the above-given truth table if any value of the operand is “false”, then after evaluating the expression, the AND operator “&&” will return a “false” value, in case both values are set as “true,” the AND operator will mark the expression as “true”.

## Example: How to use JavaScript Boolean AND operator

In the following example, the boolean variable “x” comprises a “true” value and “y” is initialized with “false”:

let x = true,

y = false;

console.log(x && y);

The “x && y” expression will return “false” after checking the value of the “y” variable:

In the other case, when both of the variables contain “true” as their value, then the Boolean AND operator will also return “true”:

let x = true,

y = true;

console.log(x && y);

Output

## How to use JavaScript Boolean NOT operator

The JavaScript Boolean NOT operator is used to inverse the value of an operand, and it is represented by an exclamation mark “!”.

### Syntax of JavaScript Boolean NOT operator

!x

Here, the NOT operator will inverse the value of the “x” variable in such a way that if its value is “true”, the NOT operator will change it to “false.” If it already has a “false” value, then the resultant value of “!x” will be “true”.

### Truth Table of JavaScript Boolean NOT operator

Based on the values of the “x” variable, the corresponding “!x” will work as follows:

 x !x undefined true null true NaN true Object {} false Empty string “ “ true Non-empty string false Number other than 0 false

## Example: How to use JavaScript Boolean NOT operator

Now, we will utilize the JavaScript Boolean NOT operator to inverse the values of the given “x” and “y” boolean variables:

let x = true,

y = false;

console.log(!x);

console.log(!y);

As you can see from the output, the values returned by the Boolean NOT operator are opposite to the original values of “x” and “y”:

That was all essential information related to the usage of JavaScript Boolean Operators. Explore them further according to your preferences.

## Conclusion

The JavaScript Boolean OR (||) operator, Boolean AND operator (&&), and Boolean NOT (!) operator are used to perform different types of comparison on the specified variables and evaluate the results. The JavaScript NOT operator inverse the value of a boolean variable, whereas the return case of the Boolean OR and AND operators depend upon the specified variables’ values. This write-up explained the usage of the JavaScript Boolean operators.