Short circuit evaluation
Tags: programming, javascript, tutorial
Published: 2021-10-28 00:53:13

## What is short circuit evaluation?

In Programming, many languages use what’s called short circuit evaluation. Short circuit evaluation is the concept of skipping the evaluation of the second part of a boolean expression in a conditional statement when the entire statement has already been determined to be either true or false.

Therefore checking the part of the expression that comes after what made the statement true or false becomes unnecessary. The compiler just skips over it and moves on to the next line of code within the statement if it evaluates to true or exits the conditional statement if it evaluates to false.

## Implementing short circuit evaluation in Javascript

To implement short circuit evaluation let’s try it with this conditional statement below as an example.

```javascript
let isTimeToCode = true;
let needsCoffee = true;

if (!isTimeToCode && needsCoffee) {
    console.log("Make some coffee! It's time to code!");
}
```

First, we’re setting both `isTimeToCode` and `needsCoffee` variables to true. Now in the expression, we’re including the `!` (pronounced as the logical NOT operator) making our `isTimeToCode` variable point to false instead of true. Since our boolean expression (`!isTimeToCode && needsCoffee`) now says false and true instead of true and true, the `needsCoffee` part of the expression will be skipped over as there is no need to check it because the condition would evaluate as false no matter what. *So it looks like we won’t be making coffee and coding today! :(*

## Avoiding errors with short circuit evaluation

Let’s get into a bit more complex examples to show where short circuit evaluation can really come in handy.

For example, we’ll take this Javascript object containing names of seasons during the year.

```javascript
const seasons = {
    "Spring": { isMonth: (month) => month },
    "Summer": { isMonth: (month) => month },
    "Winter": { isMonth: (month) => month }
}
```

Now take the conditional statement below where we are checking for a season and a month in our `seasons` object.

```javascript
if (seasons["Fall"].isMonth("October")) {
    makeCoffee("Pumpkin Spice");
}
```

What do you think will happen here since `"Fall"` is not in our `seasons` object? It’s going to throw a [TypeError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError).

`Uncaught TypeError: Cannot read properties of undefined (reading 'isMonth')`

This is a situation where we can take advantage of short circuit evaluation. Let’s modify our expression to first check if `"Fall"` is in our `seasons` object, then call its `isMonth()` method on it.

```javascript
if (seasons["Fall"] && seasons["Fall"].isMonth("October")) {
    makeCoffee("Pumpkin Spice");
}
```

Here we’re using short circuit evaluation, so if our seasons does not contain `"Fall"` we’ll ignore the second half of the conditional and avoid getting a TypeError.

## Summary

Short circuit evaluation is a helpful way to avoid performing extra or unnecessary tasks in your code. It can also be helpful with avoiding errors in certain cases such as trying to perform an action on some data that doesn’t exist.

*For more on this topic, check out the resources I’ve included below.*

* [Short Circuit Evaluation (Interview Cake)](https://www.interviewcake.com/concept/javascript/short-circuit-evaluation?course=fc1&section=general-programming)

* [Short-circuit evaluation in programming (geeks for geeks)](https://www.geeksforgeeks.org/short-circuit-evaluation-in-programming/)

* [Short-circuit evaluation (Wikipedia)](https://en.wikipedia.org/wiki/Short-circuit_evaluation#:~:text=Short%2Dcircuit%20evaluation%2C%20minimal%20evaluation,the%20expression%3A%20when%20the%20first)