UX Design and Development course

Fun with nesting if/else and comparison operators

Set some global vars

var apple = 20;
var orange = 99;
var cherry = 99;

Create an if statement that has nested if statements

if (apple != orange) {
  if (orange == cherry) {
    console.log('I have ' + (orange + cherry) + ' oranges and cherries!');
  } else {
    console.log('I have ' + (apple + orange + cherry) + ' apples, oranges and cherries in my basket');
  }
}

Short hand for nesting complex conditions

if ((apple < orange) && (orange == cherry)) {
  console.log('I got fruit!');
}

Shorthand for OR condition using ||

if ((apple < orange) || (apple == orange)) {
  console.log('I have apples and oranges');
}

Play with code

JS Bin