Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Don’t Use If-Else and Switch in JavaScript, Use Object Literals

function getDrink (type) {
  var drinks = {
    'coke': 'Coke',
    'pepsi': 'Pepsi',
    'lemonade': 'Lemonade',
    'default': 'Default item'
  };
  return 'The drink I chose was ' + (drinks[type] || drinks['default']);
}

var drink = getDrink('coke');
// The drink I chose was Coke
console.log(drink);
Source by ultimatecourses.com #
 
PREVIOUS NEXT
Tagged: #Use #Switch #Use #Object #Literals
ADD COMMENT
Topic
Name
7+9 =