Open In App

Javascript Short Circuiting Operators

Last Updated : 26 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 avoiding unnecessary computations.

Types of Short-Circuiting:

1. AND(&&) Short Circuit

In an expression using the logical AND operator (&&), evaluation stops as soon as a false result is encountered. This is because, in the case of &&, if any operand evaluates to false, the entire expression will be false, regardless of the remaining conditions.

When JavaScript encounters an expression with &&, it evaluates from left to right:

  • If the first operand is false, it short-circuits and returns false, skipping any further evaluations.
  • If the first operand is true, it proceeds to evaluate the next condition.

Short-circuiting in JavaScript can also be used to replace if-else statements. For example, true && expression always evaluates to the expression, and false && expression always evaluates to false.

Example 1: Below is an example of the Short circuiting operators.

javascript
const result = false && (console.log("This won't run"), true); 
console.log(result); 

Output
false

 Example 2: Short-circuiting using the AND(&&) operator. 

javascript
const a = 5;
const b = 10;
const result = (a > 0 && b < 20) && "Both conditions are true";
console.log(result);

Output
Both conditions are true

2. OR(||) Short Circuit

In an expression using the logical OR operator (||), evaluation stops as soon as a true result is encountered. This is because if any operand is true, the entire expression will be true, regardless of the remaining conditions.

When JavaScript encounters an expression with ||, it evaluates from left to right:

  • If the first operand is true, it short-circuits and returns true, skipping further evaluations.
  • If the first operand is false, it proceeds to evaluate the next condition.

Like &&, OR short-circuiting can be used as a replacement for if-else statements. For example, true || expression always returns true, and false || expression always returns the expression.

Example: Short-circuiting using OR(||). 

javascript
const result = true || (console.log("This won't run"), false);
console.log(result); 

Output
true

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



Next Article

Similar Reads