Switch keyword - setting default statement

I am working on a switch keyword exercise:

Here is code for switch statements to award an athlete with an appropriate medal

let athleteFinalPosition = '';

switch (athleteFinalPosition) {

case 'first place':

console.log('You get the gold medal!');

break;

case 'second place':

console.log('You get the silver medal!');

break;

case "third place":

console.log('You get the bronze medal!');

break;

default:

console.log('No medal awarded.');

break;

}

Question:
For the default statement at the end, why couldn’t we do the following? Why did we write default instead?

case ' ':
 console.log('No medal awarded.');
 break;

You could do that, which is fine. The overall purpose of default: is to catch all other edge cases. The switch should only accommodate necessary cases, without consideration of possible cases that would fail. Default is the intuitive approach, rather than introducing arbitrary imperatives.

Also note, as the final case in the switch, break is superfluous. It breaks at the end of the switch, anyway.

  default: console.log("Nothing happened.")
}
2 Likes

The overall purpose of default: is to catch all other edge cases .

  • Thanks for explaining this!

Also note, as the final case in the switch, break is superfluous.

  • Noted, thanks!
1 Like