Files
Amy Lam 914a7c522d Copy edits (#35536)
* Copy edits to Basic JavaScript section

* Copy edits to ES6 section

* Update index.md
2019-03-29 22:06:58 +05:30

520 B

title
title
Adding a Default Option in Switch Statements

Adding a Default Option in Switch Statements

  • Adding a default option makes sure that in case your variable doesn't match any of the options, the default will be used.

Solution:

function switchOfStuff(val) {
  var answer = "";
  
  switch (val){
    case 'a': answer = 'apple'; 
    break;
    case 'b': answer = 'bird'; 
    break;
    case 'c': answer = 'cat'; 
    break;
    default: answer = 'stuff';
  }
  
  return answer;  
}