🔰short-circuiting
JS ⟩ statement ⟩ expression ⟩ operator ⟩ term ⟩ short-circuiting
a && b // b may not be evaluated
a ?? b // (same)
condition ? a : b // only one of a and b is evaluated
obj ?. prop // prop may not be evaluated
f ?. (args) // args expressions may not be evaluatedlogical operator (&&, ||) - a && b, a || b
nullish coalescing (??) - a ?? b
conditional operator (?:) - condition ? a : b
optional chaining (?., ?.[]) - obj ?. prop, obj ?. [prop]
optional invocation ?.() - f ?. (args)
replit:short-circuiting (optional invocation)
let f = null; // ⭐ not a function
let x = 0;
try {
// 1. `x++` evaluated first. ⭐
// 2. TypeError: f is not a function ⛔
f(x++);
} catch(e) {
log(e.name, e.message);
log(x); // 1 (x is incremented)
}
// ⭐ short-circuiting (`x++` not evaluated)
f ?. (x++), // undefined (no error)
x, // 1 (x not incremented)Last updated
Was this helpful?