JavaScript Conditionals for Pythonistas (Summary)

Congratulations, you made it to the end of the course! Ruth’s race-day call now runs in JavaScript. On the main morning, race-day-call.js posts the same four lines as race_day_call.py, starting with “Saturday morning race: racing (good racing wind)” and “Course: harbour triangle”.

In this course, you learned how to:

  • Write an else if chain with each branch in braces, and declare let decision; and let reason; above it
  • Give a const variable one of two values with the conditional operator, condition first, as in startHour < 12 ? "morning" : "afternoon", and drop the value into ${}
  • Combine conditions with &&, ||, and !, as in decision === "racing" && !officerConfirmed, and negate a whole comparison with !== or !(...)
  • Check a range with two full comparisons, 6 <= windKnots && windKnots < 25
  • Ask about one of several values with ["thunder", "fog"].includes(forecast) and about emptiness with warnings.length > 0
  • Match one value with switch, break, stacked cases, and default
  • Look a value up with COURSE_FOR[direction] ?? "short windward-leeward" when every case only picks a value

The order of Ruth’s rules carried over as written, since JavaScript also tries the conditions from the top and the first truthy one wins. The shape is what changed, because JavaScript doesn’t read indentation.

In Python, a name assigned in a branch is still there after the chain. In JavaScript, a variable declared with const inside a branch is available only there. So you declare decision and reason above the chain with let because each branch assigns them later. Type elif, and, or a conditional expression in Python’s order, and JavaScript stops the file with a SyntaxError that points at the line, which makes those the friendly mistakes.

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

Become a Member to join the conversation.