In JavaScript’s short circuit feature, an expression is evaluated from left to right until it is confirmed that the results of the remaining conditions will not affect the already evaluated result. If the result is clear even before the expression is fully evaluated, this is ignored, and the result is returned. Short circuit assessment avoids unnecessary work and leads to effective treatment.


Please Check all JavaScript articles HERE. For more such post you can refer HERE.

AND (&&) short circuit: In the case of AND, the expression is evaluated until we get a false result because the result will always be false, regardless of other conditions. If there is an expression with && (logical AND) and the first operand is false then a short circuit occurs, the additional expression is not evaluated and false is returned. Shortening JavaScript is also useful because it can be used to replace if else statements. In JavaScript, the expression true && always evaluates to expression and the expression false && always evaluates to false, regardless of whether the expression returns true/false or not.

Example: Below is an example of the short-circuiting operators.

JavaScript

<script>
    function test() {
        // AND short circuit
        console.log(false && true)
        console.log(true && true)
        // OR short circuit
        console.log(true || false)
        console.log(false || true)
    }
    test();
</script>

Output:

false
true
true
true

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

JavaScript

<script>
    // Since first operand is false and operator
    // is AND, Evaluation stops and false is
    // returned.
    console.log(false && true && true && false)
    
    // Whole expression will be evaluated here.
    console.log(true && true && true)
</script>

Output:

false
true

OR(||) short circuit: In the case of OR, the expression is evaluated until we get a true result because the result will always be true, regardless of other conditions. If there is an expression with || (logical OR) and the first operand itself is true, a short circuit occurs, evaluation stops, and the return value is true. The abbreviation OR can also be used to replace if else statements, just like the abbreviation AND in JavaScript. In JavaScript, the expression true|| always returns true and false || expression always returns expression.

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

JavaScript

<script>
    // First operand is true and operator is ||,
    // evaluation stops and true is returned.
    console.log(true || false || false)
    
    // Evaluation stops at the second operand(true).
    console.log(false || true || true || false)
</script>

Output:

true
true

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

Whether you are preparing for your first job interview or want to improve your skills in this ever-changing technology landscape, codeconfig.in courses are your key to success. We offer free premium content, all aimed at accelerating your growth within a limited time frame. Join the millions of people we’ve helped and we’re here to do the same for you. Don’t miss this opportunity – check it out now!

Explore More

On click of parent page pop up window should stay on top only

To ensure the popup window stays on top of the parent page, you can use the window.open() method with specific focus-handling logic. JavaScript allows you to bring the popup window

Open a Fixed-Size Popup Without Minimize/Maximize Effects

Disabling the minimize and maximize buttons of a popup window is not directly supported in modern browsers, including Microsoft Edge, due to security and user-experience considerations. The window.open() function provides

How to remove JavaScript Objects from memory

To remove unused objects and improve memory management in JavaScript, you generally rely on the JavaScript engine’s garbage collection mechanism. However, you can aid this process by explicitly removing references