Understanding Short-circuit Evaluation in Javascript
Explore about Short-circuiting in Javascript to make your code clean and beautiful.

In short-circuit evaluation, an expression with logical operators ( || and && ) is evaluated from left to right. So, if the condition is met and the rest of the conditions won’t affect the already evaluated result, the expression will short-circuit and return that result.

Logical OR(||) Operator

This operator will return the first true value. So, it won’t even reach the rest of the conditions and return true as the condition is satisfied.

true || false

// true

So how can we use this?

Let’s take an example

var x;

var y = 'Gurseerat';

var z = 'This will not evaluate'; var name = x || y || z;

console.log(name);

// Gurseerat
['Gurseerat Kaur']
['JavaScript', 'Programming', 'Technology', 'Coding', 'ES6']