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

A quote’s first note starts its list, so that call leaves notes out. Python’s add_note() uses the familiar idiom, notes=None and a fresh list inside, since a list default would be shared by every call. The line-by-line port turns None into null and drops the =None, since a JavaScript call can already leave any argument out:

Language: JavaScript
function addNote(note, notes) {
  if (notes === null) {
    notes = [];
  }
  notes.push(note);
  return notes;
}

console.log(addNote("rush job"));

It throws at the .push() line. Only the error line is shown:

Language: Program Output
TypeError: Cannot read properties of undefined (reading 'push')

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:00 Each quote collects notes, and its first call leaves the list out. Python’s add_note() defaults notes to None and makes a fresh list inside, since a list default would be shared by every call.

00:12 In this lesson, you’ll port that idiom and give JavaScript parameters their defaults.

00:19 The port turns None into null, its JavaScript look-alike, and carries the rest across. The = None only made notes optional, and JavaScript lets a call leave any argument out, so the port drops it.

00:33 The last line logs Bauer Legal’s first note. What happens? Your turn: pause the video and predict.

00:46 It throws a TypeError at the .push() line. The message says undefined, not null.

00:55 You’ve seen it with rush in workingDays(): a missing argument is undefined. But JavaScript never fills in null for one, so the check is false.

01:06 Python’s None means both nothing given and empty on purpose. JavaScript has two values: undefined for the first, null for the second.

01:18 JavaScript has defaults too, and one fills in whenever the argument is undefined. So write the default you mean, an empty array, and the check drops out.

01:29 Run it: rush job, in an array. Watch out: unlike Python’s, a JavaScript default is 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.

01:50 The quote’s defaults keep Python’s shape: the signature and body port row for row. JavaScript style puts spaces around each =, and false is lowercase.

02:02 workingDays() gets rush = false too. In Python, that default lets a call leave rush out; JavaScript already allowed that, so here it says a missing rush is a real false, not an undefined that happens to work.

02:17 Run both with only a word count. The contract gets its usual fee and five days. Café Linde’s menu gets the minimum.

02:28 That’s the JavaScript way, in declarations and arrow functions alike: write the default you mean, even an array. It fills in for undefined, and a missing argument is undefined, never null.

02:43 Next up is the exercise Collect the Notes on a Quote, where you’ll make each quote collect its own notes.

Become a Member to join the conversation.