Switch Statement

switchcasedefault, and break.
This switch statement is used for multiple branching logic, where all cases are contained inside one block of code, marked out with the keyword case.

switch (day) {
  case 0:
    console.log('Sunday');
    break;
  case 1:
    console.log('Monday');
    break;
  case 2:
    console.log('Tuesday');
    break;
  case 3:
    console.log('Wednesday');
    break;
  case 4:
    console.log('Thursday');
    break;
  case 5:
    console.log('Friday');
    break;
  case 6:
    console.log('Saturday');
    break;
  default:
    console.log('Invalid Day');
    break;
}

break;
Use to break out of a conditional or a loop.
This statement must exist in each case to exit the switch block once the code is executed. Otherwise if it’s missing, the statement will fall through to the next code.

This is a reason why many developers consider the switch statement to be a bad choice to make when branching inside a program and isn’t considered a bad practice.

default:
Acts the way an else block acts, in an if else statement.


Ternary Operator

condition?statement1:statement2;

The ternary operator a is a very compact, two-way branch that many programmers use to write very concise code.

var isTrue = true;

isTrue ? console.log('yes') : console.log('no');

Refactoring the previous example

var isTrue = true;

var yesOrNo = isTrue ? 'yes' : 'no';
console.log(yesOrNo);