The JavaScript logical operators are used to combine two or more conditions to make decisions. It uses the Boolean values of the two (multiple) expressions and performs logical operations on those true and false values. These are widely used in if statements, loops, or any conditional statements.
There are four types of logical operators in JavaScript, and each has its own significance. They are && (Logical AND), || (Logical OR), ! (Logical NOT), and the Nullish Coalescing (??) operator.
JavaScript Logical Operators List
The table below describes four logical operators with a basic description and an example.
| OPERATORS | NAME | DESCRIPTION | EXAMPLE |
|---|---|---|---|
| && | Logical AND operator | It will return True when both conditions are true | If (age > 18 && age <=35) |
| || | Logical OR operator | It will return True when at least one of the conditions is true | If (age > 35 || age < 60) |
| ! | Logical NOT operator | If the condition is True, the NOT operator makes it false | If age = 18 then, !( age = 18) returns false. |
| ?? | Nullish coalescing | If the left operand is null, it returns the value of the right operand. Otherwise, it returns the left operand. | null ?? 10 |
The Comparison Operators are used to compare two variables. What if we want to compare more than one condition? Very simple, Logical Operators will do the trick for you. Let us check the truth tables and examples behind JavaScript Logical Operators for a better understanding.
Logical AND Operator (&&)
The logical AND operator, denoted by the && symbol, checks whether both operands are true. If both left and right operands are true, it returns true. If either of the two expressions is false, the logical && operator returns false.
The following table shows the truth table of the logical AND operator.
| Condition 1 | Condition 2 | Condition 1 && Condition 2 |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
NOTE: If either of the two operands is 0, the JavaScript logical AND operators returns 0 as the output.
Example
Generally, we use logical AND to check multiple expressions. If we use it on static values, it displays the last (right-most operand value) as the output.
In the first statement, both x and y are true, so it returns true and displays the value from the right side. Within the second statement, the right side operand is 0, so it returns 0.
let x = 15;
let y = 0;
let z = 10;
console.log(x && z);
console.log(x && y);
10
0
JavaScript Logical AND Operators example
In the following example, the if condition checks whether the username is admin and the status is active. If both conditions are true, it display Login successful message. When you try to change the username or isActive status, it returns a ” Login failed message.
let user ='admin';
let isActive =true;
if(user == 'admin' && isActive){
console.log("Login Successful");
}
else {
console.log("Login failed");
}
Login Successful
JavaScript Logical OR Operators (||)
The logical OR operator, denoted by the || symbol, checks whether at least one of the operands is true. If there is one operand that returns True, the output becomes True. If both operands are false, the logical OR operator returns false.
The truth table behind the logical OR operator is
| Condition 1 | Condition 2 | Condition 1 || Condition 2 |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Example
Generally, we use the JavaScript logical OR operator to check multiple expressions. However, if we use it on static numeric values, it returns the first (left) operand as the output. If the left operand is zero, it returns the value of the right operand.
let x = 15;
let y = 0;
let z = 10;
console.log(x || z);
console.log(x || y);
console.log(y || z);
15
15
10
Logical OR Operator example
In the following example, we use the Logical OR to check multiple expressions. The first expression checks whether the age is less than 18, and the second checks age is greater than 60. If any of the condition evaluated to true, the person is not eligible to work.
let age = 17;
if(age < 18 || age > 60){
console.log("Not fit for the job");
}
else {
console.log("Apply for the Job!");
}
Not fit for the job
If you change the age value above 60, it returns the same result. If the age is between 18 and 60, it returns Apply for the job.
JavaScript Logical NOT Operators (!)
The logical NOT operator, denoted by the (!) symbol, converts the given value or expression to a Boolean value and returns the opposite of it.
Syntax: !a
Example
let a = true;
console.log(!a);
false
Logical NOT operator example
Similar to the other logical operators, we can use the logical NOT inside the if-else condition. The condition inside the example below checks whether the person’s age is greater than 18.
The logical NOT operator takes the Boolean value of the evaluated expression result and returns the exact opposite. Below, the condition (age > 19) is evaluated as false. However, the not operator converts it to true and prints the message inside the block.
let age = 12;
if (!(age > 18)) {
console.log("Minor to Vote");
}
else {
console.log("Eligible to Vote");
}
Minor to Vote
TIP: You can use the double negation (!!) to reverse the result.
JavaScript Nullish Coalescing Operator (??)
The nullish coalescing logical operator in JavaScript, denoted by the ?? symbol, is useful for checking a null operand. If the left operand is null or undefined, it returns the right operand value as output. If the left operand is not null, it returns the left operand value.
Syntax: a ?? b
Nullish Coalescing Operator Example
In the following example, the nullish coalescing logical operator in JavaScript checks whether the year value is null. If true, print the current year value.
let year = null
let currentYear = 2026
console.log(year ?? currentYear)
2026
If we change the year value to something other than null, it always returns the value of the left operand.
let year = 2025
let currentYear = 2026
console.log(year ?? currentYear)
2025
Apart from the null and undefined values, the nullish coalescing operator does not consider 0 or false values and returns the value on the left side.
console.log(false ?? 10)
console.log(0 ?? 10);
false
0