What is switch in control flow


#1

i cant understand the switch



var moonPhase = 'full';

if (moonPhase === 'full') {
  console.log('Howwlll!');
} else if (moonPhase === 'mostly full') {
  console.log('Arms and legs are getting hairier');
} else if (moonPhase === 'mostly new') { 
  console.log('Back on two feet');
} else {
  console.log('Invalid moon phase');
}

switch (moonPhase) {
 case 'full':
    console.log('Howwwwlll!!');
    break;
 case 'mostly full':
    console.log('Arms and legs are getting harrier');
    break;
 case 'mostly new':
    console.log('Back on two feet');
    break;
  default:
    console.log('Invalid moon phase');
    break;
}



#2

switch is just cleaner then using if/else if and finally else. It works roughly the same, but in programming, cleanness is also very important. For another programmer looking at your code, its muck quicker clear what you are attempting to do


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.