Posts

Showing posts from August, 2015

JavaScript Short Circuiting

Today, I'll be discussing the unorthodox and not so familiar JavaScript idiom called Short Circuit (&& and ||). As developers, most of us have been taught on the popular use of the Logical AND and OR operators since our college days. Some might even learned it at the age of three!Back to Basics We need to revisit the basic usage of these operators before tackling short-circuiting in JavaScript. For additional information regarding JavaScript Logical AND and OR operators, Please visit the following links: W3 SchoolsMozilla DocumentationLogical AND Operator (&&)The AND operator (&&) is used to evaluate if both of its operands are true. If both of its operands are true, then it returns true. Otherwise, then it returns false.Example var x = 1, y = 2; console.log(x == 1 && y == 2);//returns true console.log(x == 3 && y == 2);//returns false console.log(x == 1 && y == 3);//returns…