We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Switch

Switch statements are a way to compare a variable against multiple possible values. They are similar to if-else statements, but tend to be more readable when there are many potential options.

const os = "mac";
let creator;
switch (os) {
  case "linux":
    creator = "Linus Torvalds";
    break;
  case "windows":
    creator = "Bill Gates";
    break;
  case "mac":
    creator = "Steve";
    break;
  default:
    creator = "Unknown";
    break;
}

console.log(creator);
// Steve

Unlike some languages where fall-through doesn't happen by default, JavaScript will continue to execute the next case until it reaches a break or return statement.

99 times out of 100, you'll want to include a break/return statement after each case to prevent this behavior.

Assignment

Fix the bug in the billingCost function. The "basic" plan is set correctly, but we need matches for the "pro" and "enterprise" plans, too. If the plan is:

  • "pro", the cost should be 20.0
  • "enterprise", the cost should be 50.0.

Test your function with the provided cases to ensure correctness.