JavaScript tutorials > JavaScript Basics > Operators > What are logical operators in JavaScript?
What are logical operators in JavaScript?
This tutorial explains the logical operators in JavaScript: AND (&&), OR (||), and NOT (!). You'll learn how they work, see practical examples, and understand their common use cases.
Introduction to Logical Operators
Logical operators are fundamental building blocks in JavaScript for making decisions based on multiple conditions. They allow you to combine or negate boolean expressions (true or false values), enabling more complex and flexible logic in your code. JavaScript provides three primary logical operators: AND (&&
), OR (||
), and NOT (!
).
The AND (&&) Operator
The AND operator (&&
) returns true
if and only if both operands are true
. Otherwise, it returns false
. In the example, both x < 10
and y > 5
must be true for the if
block to execute.
let x = 5;
let y = 10;
if (x < 10 && y > 5) {
console.log("Both conditions are true");
}
Concepts Behind the AND Operator
The AND operator uses short-circuit evaluation. This means that if the first operand evaluates to false
, the second operand is not evaluated because the overall expression will always be false
regardless of the second operand's value. This can improve performance and prevent errors.
The OR (||) Operator
The OR operator (||
) returns true
if at least one of the operands is true
. It returns false
only if both operands are false
. In the example, either a > 5
or b < 2
(or both) must be true for the if
block to execute. Since b < 2
is true, the code executes.
let a = 2;
let b = 1;
if (a > 5 || b < 2) {
console.log("At least one condition is true");
}
Concepts Behind the OR Operator
Similar to the AND operator, the OR operator also uses short-circuit evaluation. If the first operand evaluates to true
, the second operand is not evaluated because the overall expression will always be true
regardless of the second operand's value. This can lead to performance benefits.
The NOT (!) Operator
The NOT operator (!
) is a unary operator that negates the value of its operand. If the operand is true
, the NOT operator returns false
, and vice versa. In the example, !isTrue
evaluates to false
.
let isTrue = true;
let isFalse = !isTrue;
console.log(isFalse); // Output: false
Real-Life Use Case Section: Form Validation
Logical operators are frequently used in form validation to ensure that all required fields are filled in correctly. The example above checks if both the name and email fields are populated and that the email field contains an '@' symbol. Only if all these conditions are met is the form considered valid.
function validateForm(name, email) {
if (name && email && email.includes('@')) {
console.log("Form is valid");
return true;
} else {
console.log("Form is invalid");
return false;
}
}
validateForm("John Doe", "john.doe@example.com"); // Form is valid
validateForm("", "john.doe@example.com"); // Form is invalid
validateForm("John Doe", "john.doeexample.com"); // Form is invalid
Best Practices
When to use them
Logical operators are used in almost any JavaScript program. They are extremely useful when dealing with any form of boolean conditions. Here are some of the scenarios when logical operators are indispensable:
Interview Tip
Be prepared to explain short-circuit evaluation and how it can be used to optimize code. Also, understand the truth tables for AND, OR, and NOT operators.
Memory footprint
Logical operators themselves have a negligible memory footprint. Their impact on memory depends on the complexity of the expressions they are used in and the data types of the operands. Simple boolean operations consume very little memory.
Alternatives
While there aren't direct *alternatives* to logical operators themselves for performing boolean logic, you can sometimes restructure code to avoid deeply nested logical expressions. For example, using a switch statement or a lookup table might simplify certain complex conditional scenarios.
Pros
Cons
FAQ
-
What is short-circuit evaluation?
Short-circuit evaluation is a feature of AND (&&
) and OR (||
) operators where the second operand is not evaluated if the result of the expression can be determined from the first operand alone. For AND, if the first operand isfalse
, the expression isfalse
. For OR, if the first operand istrue
, the expression istrue
. -
Can I use logical operators with non-boolean values?
Yes, JavaScript's logical operators can be used with non-boolean values. JavaScript will implicitly convert these values to boolean values according to these rules:- Values like
0
,null
,undefined
,NaN
, and the empty string (""
) are consideredfalse
. - All other values are considered
true
.
- Values like
-
How can I combine multiple conditions effectively?
Use parentheses to group conditions and ensure the correct order of evaluation. Break down complex expressions into smaller, more manageable parts using intermediate variables if necessary. Add comments to explain complex logic.