From Dicts to JavaScript Objects for Pythonistas (Summary)

Congratulations, you made it to the end of the course! Marta’s label maker now runs in JavaScript. For order 1042, shipping-label.js picks the Leeds billing address, since the shipping address is null, and prints “no phone” for a guest checkout. Its label quotes £3.20 for Britain, counts two parcels, keeps the insured value at 0, and lists the tags as ["fragile", "gift"], exactly as shipping_label.py does.

In the last exercise, you added the remote-area surcharge and the price for orders that aren’t gifts, so order 1043’s label shows £7.70 in postage and a price of £18.50. In this course, you learned how to:

  • Build an object literal with bare keys and read its properties with dot notation when you know the key and with bracket notation when the key comes from data, as in RATES[country]
  • Put a key held in a variable into a literal with [HOME_COUNTRY]: 3.2, since a bare key uses its literal name
  • List keys, values, and pairs with Object.keys(), Object.values(), and Object.entries(), and pick a Map when keys must stay numbers
  • Give a value that’s undefined or null a default with ??, so order 1042 keeps its insured value of 0, and remember that unlike .get(), ?? replaces a stored null too
  • Reach into a nested object that may be missing with ?., as in order.customer?.phone ?? "no phone"
  • Drop repeated tags with new Set(), check membership with .has(), and spread a Set back into an array with [...tags]
  • Read pasted JSON with JSON.parse() and turn the label into indented JSON text with JSON.stringify(label, null, 2)

Keep Asking What Could Be Missing

Python tells you loudly when a key isn’t there. order["phone"] raises a KeyError. JavaScript stays quiet. A missing key reads as undefined, and it travels on until something fails further down or it lands on a label.

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.