All you need to know About short ciruting(&& and ||) in Javascript
If you enjoy this topic, you will probably like my articles. If you’re wondering, check out my social media profiles, and don’t forget to follow me since I’m offering programming and motivating tools and information to help you achieve your goals.
In JavaScript, short circuits are a common technique used to simplify code and make it more efficient. Short circuits allow us to write code that evaluates an expression only when necessary, instead of evaluating all expressions regardless of whether they are needed. In this blog post, we will discuss what short circuits are, how they work, and when to use them.
1-The logical AND (&&
) (logical conjunction) operator for a set of boolean operands will be true
if and only if all the operands are true
. Otherwise it will be false
.
2-The logical OR (||
) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value. However, the ||
operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.
What are Short Circuits?
Short circuits are boolean expressions that are evaluated from left to right. The evaluation stops as soon as the result can be determined. If the left operand of an OR (||) operator is true, the right operand is never evaluated. Similarly, if the left operand of an AND (&&) operator is false, the right operand is never evaluated.
How do Short Circuits Work?
Short circuits work by taking advantage of the fact that JavaScript evaluates boolean expressions lazily. That is, JavaScript only evaluates a boolean expression to the minimum extent necessary to determine its truth value. This means that if the left operand of an OR operator is true, the right operand is never evaluated because the entire expression is already true. Similarly, if the left operand of an AND operator is false, the right operand is never evaluated because the entire expression is already false.
When to Use Short Circuits?
Short circuits are most commonly used when checking for the existence of a property or variable. For example, if we want to check if a variable is defined before using it, we can use a short circuit like this:
if (myVariable != null && typeof myVariable === "string") {
// Do something with myVariable
}
In this example, the right operand of the AND operator is only evaluated if the left operand is true. If myVariable is null, the expression will short circuit and the right operand will not be evaluated.
The OR operator (||)
true || true; // t || t returns true
false || true; // f || t returns true
true || false; // t || f returns true
false || 3 === 4; // f || f returns false
"Cat" || "Dog"; // t || t returns "Cat"
false || "Cat"; // f || t returns "Cat"
"Cat" || false; // t || f returns "Cat"
"" || false; // f || f returns false
false || ""; // f || f returns ""
false || varObject; // f || object returns varObject
Logical AND (&&)
a1 = true && true; // t && t returns true
a2 = true && false; // t && f returns false
a3 = false && true; // f && t returns false
a4 = false && 3 === 4; // f && f returns false
a5 = "Cat" && "Dog"; // t && t returns "Dog"
a6 = false && "Cat"; // f && t returns false
a7 = "Cat" && false; // t && f returns false
a8 = "" && false; // f && f returns ""
a9 = false && ""; // f && f returns false
Conclusion
Short circuits are a powerful tool in JavaScript that allow us to write more efficient and concise code. By taking advantage of JavaScript’s lazy evaluation of boolean expressions, we can simplify our code and avoid unnecessary computations. Short circuits are especially useful when checking for the existence of a property or variable. So next time you’re writing JavaScript code, consider using short circuits to make it more efficient and elegant.