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

Giving Parameters Defaults

Giving Parameters Defaults

In this lesson, you gave the translator’s JavaScript functions their defaults and saw why Python’s None idiom doesn’t carry over.

The quote form sends only the options a client ticked, so most calls leave something out. The notes on a quote work the same way: the first note starts a quote’s list, so that call passes only the note, and later notes pass the list back in to add to it. Here’s the Python helper:

Language: Python
def add_note(note, notes=None):
    if notes is None:
        notes = []
    notes.append(note)
    return notes

print(add_note("rush job"))

It gives notes a default of None and starts a fresh list inside. That’s the idiom because Python builds a default once, when the def line runs, so a list default would be shared by every call.

The line-by-line port turns None into null and is into ===, assuming null is JavaScript’s None. It drops the =None, since a JavaScript call can already leave any argument out:

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:02 The quote now hands back its fee and turnaround together. But the quote form sends only the options a client ticked, so most calls leave something out. In this lesson, you’ll give JavaScript parameters defaults.

00:18 The first note on a quote, such as "rush job", starts its list, so that call passes only the note. Later notes pass the list back in. The Python script’s add_note() gives notes a default of None, then makes a fresh list inside.

00:35 That’s the idiom, because Python builds a default once, when the def line runs, so a list default would be shared by every call.

00:45 In the JavaScript port, None becomes null, which looks like JavaScript’s None, so a missing notes ought to arrive as null.

00:53 The check comes across line for line.

00:57 append() becomes .push(). The =None only made notes optional, and a JavaScript call can already leave any argument out.

01:05 So the port drops it. The last line tries to log Bauer Legal’s first note. What happens? Your turn: pause the video and predict.

01:22 It throws a TypeError instead.

01:25 The message says undefined, not null, and the trace points at line five.

01:32 With one argument passed, notes starts out as undefined, the same value rush got when the Bauer Legal call left it out.

01:41 JavaScript never fills in null for a missing argument.

01:45 So undefined === null is false, and the check never fires. notes is still undefined at the .push() line, and reading .push from it throws.

01:57 Python uses None both for nothing given and for empty on purpose. JavaScript has two values: undefined for the first, null for the second.

02:09 JavaScript has defaults of its own, written with an equals sign, as in Python. One fills in whenever the argument is undefined, which a missing one is.

02:20 So write the default you mean: notes = [], an empty array. The check and its block drop out. Run it, and you get 'rush job' in an array.

02:32 Watch out: a JavaScript default doesn’t carry a list from call to call, as Python’s does. It’s evaluated afresh on every call that needs it. So a new quote’s first note, "legal", logs only 'legal', where a Python list default would collect both.

02:49 For a missing argument, the None check has no job. A null default would run too, but null means empty on purpose, not nothing given.

03:01 The quote’s options take defaults the same way: the rate, the rush flag, and the minimum. The JavaScript signature has Python’s shape. The body ports row for row with the Python.

03:15 JavaScript style puts spaces around each equals sign, where Python’s style leaves them out, and False becomes lowercase. Run the quote with only a word count, as the form sends it.

03:28 A 6,200-word contract with no rush ticked gets its usual fee and five days. Café Linde’s menu gets the minimum.

03:39 Python’s working_days() gained a rush default of False, which is what lets a call leave rush out. The port gets the same default.

03:49 JavaScript already accepted a call without rush, as the Bauer Legal slip showed, but rush arrived as undefined. Now it’s a real false, and anyone reading the signature sees that rush is optional and what it defaults to.

04:03 Without rush, workingDays() on its own gives five.

04:09 That’s the JavaScript way, in function declarations and arrow functions alike: write the default you mean, in the parameter list, even when it’s an array. A default fills in when the argument is undefined, and a missing argument is undefined, never null.

04:27 Next up is Collect the Notes on a Quote, an exercise. A teammate’s port keeps Python’s None idiom, and you’ll make each quote collect its own notes.

Become a Member to join the conversation.