The Wayback Machine - https://web.archive.org/web/20241001005546/https://www.geeksforgeeks.org/javascript-logical-operators/
Open In App

JavaScript Logical Operators

Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Logical operators in JavaScript are used for making decisions based on conditions and manipulating boolean values. They are commonly used to compare variables or values and set termination conditions for loops.

In JavaScript, there are basically three types of logical operators.

OPERATOR NAMEOPERATOR SYMBOLOPERATION
NOT!Converts operator to boolean and returns flipped value
AND&&Evaluates operands and return true only if all are true
OR||Returns true even if one of the multiple operands is true

1. !(NOT) Operator

!(NOT) Operator reverses the boolean result of the operand (or condition). It first converts the operand to a boolean type and then returns its flipped value.

Example: In this example, we will perform the NOT operation on number multiple times.

JavaScript
// !(NOT) operator
let i = 0;
console.log((!i));
console.log(!!i);

Output:

true
false

Explanation: Since zero is treated as a falsy value therefore NOT operation on zero will return true and when this operation is performed again we get true as output.

2. &&(AND) Operator

The && operator accepts multiple arguments and evaluates the operator from left to right. It returns true only if all the operands that are evaluated are true

Example: In this example, we will perform and operation on different numbers and evaluate their output as a boolean

JavaScript
// &&(AND) operator
let i = 0, j=2, k=3, l=8;
console.log(Boolean(i&&j&&k));
console.log(Boolean(j&&k&&l));

Output:

false
true

Explanation: In JavaScript, the value of 0 when converted to zero is considered false so when performing “and” operation on 0 a falsy value is returned and we get false output otherwise true.

3. ||(OR) Operator

The OR operator is somewhat opposite of the ‘AND’ operator. It also evaluates the operator from left to right and returns true even if one operand is evaluated as true

Example: In this example, we will perform OR operation on different data types and check their boolean output.

JavaScript
// ||(OR) Operator
let i = 1;
let j = null;
let k = undefined;
let l = 0;
console.log(Boolean(j||k));
console.log(Boolean(i||l));

Output:

false
true

Explanation: null, undefined, and 0 are recognized as falsy values. So OR operation on null and undefined will give false whereas numbers except 0 are treated as true.

Usage Examples:

  • NOT Operator: Reverses the boolean result of an operand.
  • AND Operator: Returns true only if all evaluated operands are true.
  • OR Operator: Returns true if at least one evaluated operand is true.


Supported Browsers: The browsers supported by all JavaScript Logical operators are listed below:

We have a complete list of Javascript Operators, to check those please go through this Javascript Operators Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

JavaScript Logical Operators – FAQs

What is short-circuit evaluation?

Short-circuit evaluation means that logical operators stop evaluating expressions as soon as the outcome is determined. For &&, if the first operand is false, the overall result is false, so it doesn’t evaluate the second operand. For ||, if the first operand is true, the overall result is true, so it doesn’t evaluate the second operand.

Can logical operators be used with non-boolean values?

Yes, logical operators can be used with non-boolean values. They return one of the original values instead of a boolean. For &&, if the first operand is falsy, it returns the first operand; otherwise, it returns the second operand. For ||, if the first operand is truthy, it returns the first operand; otherwise, it returns the second operand.

What are truthy and falsy values in JavaScript?

In JavaScript, a value is truthy if it is considered true in a boolean context, and falsy if it is considered false. Falsy values include false, 0, “” (empty string), null, undefined, and NaN. All other values are truthy.



Previous Article
Next Article

Similar Reads

JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &&:(AND) Evaluates operands and retu
3 min read
Logical Operators in math.js
In Math.js, we can use logical operators to perform boolean operations on values. The math.and function perform a logical AND, math.or handles logical OR, and math.not provides logical NOT. These operators allow us to evaluate boolean expressions and handle logical conditions effectively in mathematical computations. These are the following Logical
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
JavaScript Logical AND assignment (&&=) Operator
This operator is represented by x &&= y, and it is called the logical AND assignment operator. It assigns the value of y into x only if x is a truthy value. We use this operator x &&= y like this. Now break this expression into two parts, x && (x = y). If the value of x is true, then the statement (x = y) executes, and the v
2 min read
JavaScript Logical OR assignment (||=) Operator
This operator is represented by x ||= y and it is called a logical OR assignment operator. If the value of x is falsy then the value of y will be assigned to x. When we divide it into two parts it becomes x || ( x = y ). It checks if x is true or false, if the value of x is falsy then it runs the ( x = y ) block and the value of y gets stored into
2 min read
OR(||) Logical Operator in JavaScript
The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It evaluates two expressions and returns true if at least one is true, otherwise, it returns false. This operator is frequently used in conditional statements to execute code when one of multiple conditions is satisfi
1 min read
AND(&&) Logical Operator in JavaScript
The logical AND (&&) (logical conjunction) operator for a set of boolean operands will be true if and only if all the operands are true. Otherwise, it will be false. It's commonly used to combine conditions in conditional statements or to check multiple conditions before executing code. The Logical AND(&&) Operator can also be used
1 min read
Alternative to Multiple OR Operators in JavaScript
In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to perform the same task in other ways: C/C++ Code let u
4 min read
What are Comparison Operators in JavaScript ?
Comparison operators in JavaScript are used to compare two values and return a boolean result (true or false). They include operators such as ==, ===, !=, !==, >, <, >=, and <=. Example: Here, the > (greater than) the operator compares the values of a and b. Since a is not greater than b, the expression a > b evaluates to false, w
1 min read
Does JavaScript Operators like or ( || ), and ( && ), null coalescing ( ?? ) makes Weak Comparison?
JavaScript operators like logical OR (||), logical AND (&&), and null coalescing (??) are used for making conditional evaluations and performing logical operations in JavaScript. While these operators are commonly used for controlling the flow of the code execution and handling values in the different scenarios it's essential to understand
2 min read
JavaScript Operators Reference
JavaScript Operators are used to perform specific mathematical and logical computations on operands. In other words, an operator operates the operands. In JavaScript, operators are used to compare values, perform arithmetic operations, etc. Example: In this example, we will use typeof operator to check the type of variable. [GFGTABS] JavaScript let
5 min read
JavaScript Operators
JavaScript Operators are symbols used to perform specific mathematical, comparison, assignment, and logical computations on operands. They are fundamental elements in JavaScript programming, allowing developers to manipulate data and control program flow efficiently. Understanding the different types of operators and how they work is important for
11 min read
Difference Between && and || Operators in javaScript
In JavaScript, logical operators && (AND) and || (OR) are used to perform the logical operations on values. These operators are commonly used in the control flow and decision-making within the programs. Understanding the differences between these operators is essential for writing efficient and accurate code. These are the following topics
3 min read
JavaScript Comparison Operators
JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. They are used to evaluate whether a condition is true or false by comparing variables or values. These operators play a crucial role in logical expressions, helping to determine the equality or differences between values. In this article,
6 min read
JavaScript Unary Operators
JavaScript Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc. Table of Content Unary Plus (+) OperatorUnary Minus (-) OperatorUnary Increment (++) OperatorUnary Decrement (--) OperatorLogical NOT (!) OperatorBitwise NOT (~) Operatortypeof Operatord
6 min read
JavaScript Relational operators
JavaScript Relational Operators are used to compare their operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result. Types of Relational OperatorsThese are two types of relational operators, these are: JavaScript in OperatorJavaScript instanceof OperatorWe will explore all the a
3 min read
JavaScript String Operators
JavaScript String Operators are used to manipulate and perform operations on strings. There are two operators which are used to modify strings in JavaScript. These operators help us to join one string to another string. Type of JavaScript String OperatorsThere are two type of String Operators in JavaScript, these are: String Concatenate OperatorStr
2 min read
JavaScript Assignment Operators
Assignment Operators Assignment operators are used to assign values to variables in JavaScript. Syntax:data=valueExample: // Lets take some variablesx=10y=20x=y // Here, x is equal to 20y=x // Here, y is equal to 10Assignment Operators ListThere are so many assignment operators as shown in the table with the description. OPERATOR NAMESHORTHAND OPER
6 min read
JavaScript Bitwise Operators
JavaScript uses 32-bit Bitwise operands. A number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit number. Bitwise Operat
6 min read
How to use multiple ternary operators in a single statement in JavaScript ?
In JavaScript, the ternary operator (? :) is a shorthand for if-else statements. You can use multiple ternary operators in a single statement to handle multiple conditions efficiently. This makes your code shorter, but be cautious as too many nested ternary operators can reduce code readability. Syntax:condition1 ?(condition2 ? Expression1 : Expres
2 min read
JavaScript Arithmetic Operators
JavaScript Arithmetic Operators are the operator that operate upon the numerical values and return a numerical value. Arithmetic Operators listThere are many arithmetic operators as shown in the table with the description. OPERATOR NAME USAGE OPERATION Addition Operatora + bAdd two numbers or concatenate the stringSubtraction Operatora - bDifferenc
7 min read
Javascript Short Circuiting Operators
In JavaScript, short-circuiting refers to the process of evaluating expressions from left to right and stopping as soon as the result is determined. If the outcome of an expression is clear before evaluating all conditions, the evaluation is “short-circuited,” meaning the remaining conditions are ignored. This leads to more efficient processing by
2 min read
Physical and Logical Tags in HTML
Physical and Logical tags are used in HTML for better visibility and understanding of the text by the user on the web page. However, both tags differ from each other as suggested by their names. Logical Tags :Logical Tags are used in HTML to display the text according to the logical styles. Following are the Logical tags commonly used in HTML. Logi
2 min read
What are logical tags in HTML ?
Logical tags are used to tell the browser what kind of text is written inside the tags. They are different from physical tags because physical tags are used to decide the appearance of the text and do not provide any information about the text. Logical tags are used to indicate to the visually impaired person that there is something more important
5 min read
Difference between Physical and Logical Tags
In this article, we will learn about physical tags & logical tags in HTML & their implementation. We will also explore the difference between them. Let's begin the discussion with a physical tag. Physical tags: Physical tags are used to indicate that how specific characters are to be formatted or indicated using HTML tags. Any physical styl
4 min read
TensorFlow.js Operations Logical Complete Reference
TensorFlow.js is an open-source JavaScript library designed by Google to develop Machine Learning models and deep learning neural networks. TensorFlow.js Operations Logical Functions: TensorFlow.js tf.equal() FunctionTensorFlow.js tf.greater() FunctionTensorFlow.js tf.greaterEqual() FunctionTensorFlow.js tf.less() FunctionTensorFlow.js tf.lessEqual
1 min read
Less.js Logical Functions
In this article, we will see the Logical Functions in LESS.js. Logical functions are a way to perform operations and evaluate code based on logical conditions. Basically, these functions provide the power of "if-else" conditions in CSS and help us do things based on logic. There are 2 logical functions provided by Less.js which are as follows: If-f
3 min read
Less.js Logical boolean() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that improves CSS's functionality. Cross-browser interoperability is supported by LESS. A scripting language known as CSS pre-processor is used to enhance CSS and compile it in
3 min read
NOT(!) Logical Operator inJavaScript
JavaScript NOT Operator can be used to find the flipped value of a boolean. It can be used to convert a true value to a false and vice-versa. This NOT operator can also be used on non-booleans to convert them to the reverse of their actual boolean value. A NOT operator can be used with another NOT operator to get the original value back. Syntax: !a
1 min read
Logical Properties in CSS
CSS logical properties use the terms block and inline to elaborate the direction of the flow of writing. Block properties: Sets up the top and bottom properties of the webpage (for standard English text), you could say that the dimension is perpendicular to the flow of text within a line. In simpler words, the vertical dimensions in horizontal writ
4 min read