Skip to main content

Command Palette

Search for a command to run...

Short Circuiting Operators && and ||

Updated
β€’2 min read
Short Circuiting Operators && and ||
D

Hey πŸ‘‹ My name is Debasish, I'm a System Engineer and a passionate Web Developer who loves to experiment with new technologies and build projects. I like to share and showcase my tips and knowledge on this blog. Since you are here feel free to browse through some of my posts, I'm sure you will find something useful and interesting. Hope you are feeling excited!

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

More from this blog

D

Debasish Lenka

28 posts

Explore my articles on Windows Server platforms, delving into Active Directory, Azure, AWS, VMware, Networking, and security for Infrastructure building and maintenance.