What is Short-Circuit Evaluation in JS?

Logical operators (&&, ||, &, |) are used to reduce the complexity which can be used concisely.

ANDoperator (&&) returns first false for false value and last true for true values.

But,

OR operator (||) returns first true for truely statement and last false for false statement.

So, for

Falsely statement

console.log( 0 || [] );
//returns [] as it is last false 

console.log( 0 || [] );
//returns 0 as it is first false 

Truely statement

console.log( 1 || 0 || 3 );
//returns 1 as it is first true 

console.log( 1 || 2 || 3 );
//returns 0 as it is last true

NOTE:- In JS all values are true except ( 0, “”, undefined, null and false)

Bitwise operators like( |, & )

bit wise operators use to perform binary operations like bitwise or ( binay add ) and bitwise and ( binary multiplication )

so,

console.log( 3 | 2 )
// returns 3 as binary sum of 011 and 010 is 011

console.log( 3 & 2 )
// returns 2 as binary sum of 011 and 010 is 010

Leave a Reply

Your email address will not be published. Required fields are marked *