Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте switch Statements | Understanding Program Flow and Decisions
Conditional Statements and Loops in JavaScript

bookswitch Statements

Imagine you are standing in front of a vending machine filled with snacks. Each snack has a number below it. You simply press the number of the snack you want, and the machine gives it to you. This is a lot like how a switch statement works in JavaScript. You have a single value (like the number you press), and the code chooses the right action based on that value. Instead of checking every possibility with a long chain of if...else if statements, you can use a switch to make your code clearer and easier to read.

12345678910111213141516171819
// Choose a snack based on the selected number let snackNumber = 2; let snack; switch (snackNumber) { case 1: snack = "Chips"; break; case 2: snack = "Chocolate Bar"; break; case 3: snack = "Granola Bar"; break; default: snack = "Unknown snack"; } console.log("You chose: " + snack);
copy

1. When is a switch statement more useful than multiple if...else if statements?

2. Complete the switch statement so that it sets 'snack' to 'Granola Bar' when 'snackNumber' is 3.

question mark

When is a switch statement more useful than multiple if...else if statements?

Select the correct answer

question-icon

Complete the switch statement so that it sets 'snack' to 'Granola Bar' when 'snackNumber' is 3.

You chose: Granola Bar

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how the `switch` statement works in this example?

What happens if I change the value of `snackNumber`?

Are there any advantages of using `switch` over `if...else` statements?

Awesome!

Completion rate improved to 7.69

bookswitch Statements

Свайпніть щоб показати меню

Imagine you are standing in front of a vending machine filled with snacks. Each snack has a number below it. You simply press the number of the snack you want, and the machine gives it to you. This is a lot like how a switch statement works in JavaScript. You have a single value (like the number you press), and the code chooses the right action based on that value. Instead of checking every possibility with a long chain of if...else if statements, you can use a switch to make your code clearer and easier to read.

12345678910111213141516171819
// Choose a snack based on the selected number let snackNumber = 2; let snack; switch (snackNumber) { case 1: snack = "Chips"; break; case 2: snack = "Chocolate Bar"; break; case 3: snack = "Granola Bar"; break; default: snack = "Unknown snack"; } console.log("You chose: " + snack);
copy

1. When is a switch statement more useful than multiple if...else if statements?

2. Complete the switch statement so that it sets 'snack' to 'Granola Bar' when 'snackNumber' is 3.

question mark

When is a switch statement more useful than multiple if...else if statements?

Select the correct answer

question-icon

Complete the switch statement so that it sets 'snack' to 'Granola Bar' when 'snackNumber' is 3.

You chose: Granola Bar

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
some-alt