Logo

Developer learning path

JavaScript

Boolean expressions in JavaScript

Boolean expressions

9

#description

Boolean expressions in JavaScript are expressions that return a Boolean value, which can either be true or false. These expressions are typically used in conditional statements and loops to determine the flow of the program.

In JavaScript, there are several types of operators that can be used to create Boolean expressions. These include comparison operators, logical operators, and unary operators.

Comparison operators are used to compare two values and return a Boolean value that indicates whether the comparison is true or false. Examples of comparison operators in JavaScript include <, >, <=, >=, ==, and !=.

Logical operators are used to combine two or more Boolean expressions and determine the truth value of the overall expression. There are three logical operators in JavaScript: && (also known as "and"), || (also known as "or"), and ! (also known as "not").

Unary operators are used to perform operations on a single operand. One example of a unary operator used in Boolean expressions is the typeof operator, which returns a string indicating the type of the operand.

Boolean expressions are commonly used in conditional statements, such as if/else statements, to control the flow of a program.

For example:

                    
var x = 10;
if (x > 5 && x < 20) {
  console.log("x is between 5 and 20");
} else {
  console.log("x is not between 5 and 20");
}
                  

In this example, the Boolean expression x > 5 && x < 20 is evaluated to true because x is between 5 and 20. As a result, the code inside the if block is executed and "x is between 5 and 20" is printed to the console.

March 25, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.