← Back to JavaScript Course | Chapter 5: ES6+ Features | Lesson 6 of 12

JS Short Circuit Evaluation

Short circuit evaluation means JavaScript stops checking as soon as it knows the answer, like not reading the rest of a rule once it is already broken. It is often used to pick default values.
Syntax
javascript
const result = a || b;  // first truthy value
const result = a && b;  // b if a is truthy
condition && doSomething();

AND Short Circuit

&& evaluates left to right and stops as soon as it hits a falsy value, since the overall result is already determined. The right-hand expression is never evaluated in that case.

उदाहरण: AND Short Circuit

javascript
console.log(false && console.log("never runs"));

OR Short Circuit

|| stops as soon as it hits a truthy value, since that value alone determines the result. This 'short circuit' behavior means the second operand isn't always executed.

उदाहरण: OR Short Circuit

javascript
console.log(true || console.log("never runs"));

Short Circuit Function Calls

Because JavaScript skips evaluating the right side once the result is known, you can use && to conditionally call a function: isReady && doSomething() only calls doSomething if isReady is truthy.

उदाहरण: Short Circuit Function Calls

javascript
const isReady = true;
isReady && console.log("Doing something");

Default Values

|| is commonly used to supply a fallback: const name = input || Guest uses input if it's truthy, otherwise falls back to Guest — though ?? is often safer for numeric/boolean defaults.

उदाहरण: Default Values

javascript
// Declare the constant `input`, set to ""
// Declare the constant `input`, set to ""
const input = "";
// Declare the constant `name`, set to `input || "Guest"`
// Declare the constant `name`, set to `input || "Guest"`
const name = input || "Guest";
// Print `name` to the console
// Print `name` to the console
console.log(name);

Short Circuit Evaluation Rules

The short-circuit rule is what lets you chain multiple && or || conditions efficiently: evaluation stops the instant the final outcome is already decided, skipping unnecessary work.

उदाहरण: Short Circuit Evaluation Rules

javascript
const a = false, b = true, c = true;
console.log(a && b && c); // stops at a
console.log(a || b || c); // stops at b
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.