![]() |
VOOZH | about |
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.
Table of Content
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:
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.
false
Example 2: Short-circuiting using the AND(&&) operator.
Both conditions are true
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:
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(||).
true
We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.