Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Picking the Members' Note With switch

Picking the Members’ Note With switch

Under the race-day call, the members’ page posts a note for the members. Ruth’s Python script picks it with a match on the decision, and in this lesson, you ported that match to JavaScript’s switch statement.

Here’s Ruth’s step, with the decision set by hand to the main morning’s call:

Language: Python
decision = "racing"

match decision:
    case "racing":
        note = "Briefing at 9:30 in the clubhouse"
    case "postponed":
        note = "Next check at 11:00"
    case "cancelled":
        note = "See you next Saturday"

print(note)
  • Ported line by line, match turns into switch, JavaScript’s statement for one value against fixed options. The value goes in parentheses, switch (decision), and the cases go in braces. Each case carries across as Python has it, with its value, its colon and its one note line, and print(note) turns into console.log(note);.
  • As with the else if chain, let note; goes above the switch, so the note still exists after the closing brace.
  • JavaScript compares the decision with each case using ===, the same test you’d write in an if condition. For these strings, that’s the test Python’s == makes.
  • Ported line by line, with each case holding only its note, the switch runs without an error, but on a perfect racing morning, the page tells every member See you next Saturday. So does the calm morning; only the fog morning’s note is right.
  • The decision matches the first case, so execution starts there and sets the briefing note. The next case label doesn’t stop it, so execution runs on, sets the postponed note, then overwrites that with See you next Saturday. The last assignment wins.
  • Running on like this is called fall-through. Each case is an entry point, not a closed block: execution starts at the matching case and runs through every case below it until something stops it. A postponed morning skips the first case, enters at the second and falls into the canceled note. A canceled morning enters at the last case, so its note is right only because nothing sits below it.
  • In Python, a case ends at its last indented line, and only one case runs. You know break from Python’s loops, but match never needs one.
  • In JavaScript, break on a case’s last line jumps past the rest of the switch, to the line after it. End every case with one. The last case gets one too, so a case added below it later can’t fall in.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your port now posts the right call on every morning. Under the call, the members’ page also posts a note. In this lesson, you’ll port that note with JavaScript’s switch statement.

00:13 Ruth’s Python script picks the note with a match on the decision. Each case holds one call and sets the note in a single line. The snippet sets the decision by hand, to the main morning’s call, racing.

00:26 So Python prints the briefing note.

00:31 Port it line by line. The decision becomes a constant. match turns into switch, JavaScript’s statement for one value against fixed options.

00:43 The decision goes in parentheses, and the cases go in braces. Carry each case across as Python has it, with its value, its colon and its one note line. print() turns into console.log() at the bottom.

00:58 As with the else if chain, the note is declared with let above the switch, so it still exists after the closing brace.

01:06 JavaScript compares the decision with each case using ===, the same test you’d write in an if condition. For these strings, it’s the same test that Python’s == makes.

01:19 Python printed the briefing note. What does the JavaScript port print for the racing morning? Your turn: pause the video and predict.

01:33 Run it, and JavaScript prints see you next Saturday. There’s no error and no warning, but on a perfect racing morning, the page tells every member to wait for next Saturday.

01:46 The calm morning, postponed, gets the same note. The fog morning is canceled, and only its note is right.

01:57 Follow the racing morning as it runs through the switch, row by row. The decision matches the first case, so execution starts there and sets the briefing note.

02:06 The next case label doesn’t stop it, so execution runs on and sets the postponed note. Then it overwrites that with see you next Saturday. The switch ends there, and the last assignment wins.

02:23 Running on like this is called fall-through. Each case is an entry point, not a closed block. Execution starts at the matching case and runs through every case below it, until something stops it.

02:37 A postponed morning skips the first case and enters at the second. Then it falls into the canceled note. A canceled morning enters at the last case, so its note is right only because nothing sits below it.

02:52 In Python, a case ends at its last indented line, and only one case runs.

03:00 To stop execution at the end of a case, you write break on the case’s last line. It jumps past the rest of the switch, to the line after it.

03:10 You know break from Python’s loops. Python’s match never needs one, because its cases never run on. So the port ends every case with a break of its own.

03:21 The last case gets one too, so a case added below it later can’t fall in. Run it again, and the racing morning gets the briefing note, as in Python. The calm morning gets the postponed note, and the fog morning still gets see you next Saturday.

03:40 The rule to keep is that a case only marks where execution starts, so the code in every case ends with a break of its own. That’s how JavaScript developers write a switch, with the variable it sets declared above it.

03:54 In the next lesson, Sharing a Course Between Directions, you’ll port Ruth’s second match, which picks the course for the wind direction. Some of its cases cover several directions, and a last case catches the rest.

Become a Member to join the conversation.