Marta’s label needs an insured value. The courier gives every parcel its standard £50 cover unless the order says otherwise, as order 1042 does. Iris chose no cover, so the platform sends insured_value as 0. In Python, .get() with a default handles both cases in one call.
The line most Pythonistas write first in JavaScript gets order 1042 wrong without a single error. In this lesson, you’ll fill in defaults the way JavaScript developers do, count today’s orders per country, and check whether a key is really there.
Default the Insured Value
The Python script reads the insured value with .get(), so a missing key falls back to the courier’s £50:
order = {"id": 1042, "shipping_address": None, "insured_value": 0}
insured = order.get("insured_value", 50)
print(f"Insured value: £{insured}")
It prints Insured value: £0 because the key is there and its value is 0. Only an order without the key, such as order 1043, gets 50.
In JavaScript, a missing key already gives undefined instead of raising, so you don’t need .get() to stay safe, and objects don’t have one, so order.get("insured_value", 50) stops with TypeError: order.get is not a function. What you still need is the default, and ||, JavaScript’s or, looks like the natural way to say “this or else 50.” Predict what this prints for order 1042:
const order = {
id: 1042,
shipping_address: null,
insured_value: 0,
};
const insured = order.insured_value || 50;
console.log(`Insured value: £${insured}`);
Run it, and the label promises cover that Iris turned down:
Insured value: £50