LESSON 21 OF 50

Choose a branch with switch

Use switch to select a message from an exact value.

Official sourceECMAScript — switch statementReviewed: 2026-08-26
STEP 1 · LEARN

How it works

switch evaluates its expression once and finds the first case that matches strictly. break prevents fall-through into the next case, while default handles remaining values.

Correct example
const role = 'editor';
switch (role) {
  case 'admin':
    console.log('Administrare');
    break;
  case 'editor':
    console.log('Editare');
    break;
  default:
    console.log('Vizualizare');
}

The editor value activates the second case and produces one message.

Common mistake
if (role === 'editor') { console.log('Editare'); }

The result may be correct, but this exercise checks the switch structure, its cases and explicit stopping.

STEP 2 · APPLY

Your exercise

For role = “editor”, print “Editare” using switch, two cases, break and default.

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • role is declared with const.
  • switch checks role and includes admin, editor and default.
  • The output is exactly Editare.
Explain the solution logic

switch compares role with each case; break ends the editor case before default.