Short Circuiting Operators && and ||

Short Circuiting Operators && and ||

Short-Circuiting method in JavaScript helps us to avoid unnecessary processing and leads to more efficient work. JavaScript will evaluate the expression from left to right and short circuits and the result. It can use any data type, return any data type ,

This means when JavaScript evaluates an OR expression if the first value is truthy then it will immediately return the first value without evaluating the second value.

|| operator.

//if a first value is a truthy value then it will immediately return the first value without evaluate the second value
console.log(3 || "Debasish");//3
console.log('' || "Debasish");//Debasish
console.log(true || 0);//true
console.log(undefined || null);//null
console.log(undefined || 0 || '' || "Hello" || 23 || null); //Hello
// Here hello is the first truthy value in the chain and it Short circuiting the entire chain and return Hello
//Helps setting up the default value

&& operator.

When it comes to short-circuiting && works the exact opposite of ||.Returns falsy values immediately.

console.log(0 && "Debasish");//0
console.log(7 && "Debasish");//Debasish

console.log("Hello" && 23 && null && "debasish"); //null

I hope you find it useful, let me know your thoughts on this in the comments. If you have any issues or questions about it, feel free to contact me. Thank you 🌟 for reading! like, share and subscribe to my newsletter for more! 💖

🔗Debasish Lenka

Did you find this article valuable?

Support Debasish Lenka by becoming a sponsor. Any amount is appreciated!