Porting Python's Defaults and match Statements to JavaScript (Summary)

Nadia’s booking confirmations now run in JavaScript. The port prints the same twenty-five lines as the Python script, from the choir’s waived deposit to “Call before confirming: Maya Okafor, Ana Silva”.

The rules carried over, but you had to rethink each default and each match. You can now:

  • Read && and || as operators that return one of their operands, as and and or do in Python.
  • Give an empty rooms array a default with a length check, since every array is truthy.
  • Choose || or ?? field by field: || for the blank phone, ?? for the choir’s zero deposit.
  • Fill in a request in place with ??=, ||=, and &&=.
  • Port a match with guards to an else if chain, and one with patterns to a switch on the property that decides most cases, with the other checks as plain conditions.
  • Call a function from a lookup only when there’s one, with ?.(), and fall back with ??.

Keep Asking One Question

For every default you port, ask: “Are 0, "", and false real answers here?” If they are, use ??. If none of them is, as with a blank phone, use ||.

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.