5.5. Nested Conditionals

We can write code with more complex branching behavior by combining conditionals and, in particular, by nesting conditionals. Let's see how this works by tackling the following problem.

Example

Write code that prints different messages based on the value of a number variable. If the number is odd, print nothing. If it is even, print "EVEN". If it is also positive print "POSITIVE".

Our first attempt at a solution might look like this:

1let num = 7;
2
3if (num % 2 === 0) {
4   console.log("EVEN");
5}
6
7if (num > 0) {
8   console.log("POSITIVE");
9}

Console Output

POSITIVE

We find that the output is POSITIVE, even though 7 is odd and so nothing should be printed. This code doesn't work as desired because we only want to test for positivity when we already know that the number is even. We can enable this behavior by putting the second conditional inside the first.

1let num = 7;
2
3if (num % 2 === 0) {
4    console.log("EVEN");
5
6    if (num > 0) {
7        console.log("POSITIVE");
8    }
9}

Try It!

Run the previous example with several different values for num (even, odd, positive, negative) to ensure it works as desired. Nice, huh?

Notice that when we put one conditional inside another, the body of the nested conditional is indented by two tabs rather than one. This convention provides an easy, visual way to determine which code is part of which conditional.

5.5.1. Check Your Understanding

Question

What is printed when the following code runs?

1let num = 7;
2
3if (num % 2 === 0) {
4    if (num % 2 === 1) {
5        console.log("odd");
6    }
7}
  1. The code won't run due to invalid syntax
  2. odd
  3. even
  4. The code runs but doesn't print anything

Question

Considering the same conditional used in the previous question, which values of num would result in "odd" being printed?

1if (num % 2 === 0) {
2    if (num % 2 === 1) {
3        console.log("odd");
4    }
5}
  1. Even values of num.
  2. Odd values of num.
  3. No values. It is impossible for the call to console.log to ever run, given the two conditions.
  4. num is 0.