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

Passing Options by Name

Passing Options by Name

Python’s quote_line() takes a rush flag and passes it on with quote(words, rush=rush), and its caller writes quote_line("Bauer Legal", 6200, rush=True): Bauer Legal: €1,116.00, 2 working days.

The line-by-line JavaScript port keeps both keyword calls, and it stops at the bottom one with ReferenceError: rush is not defined, a message about a variable, not an argument. Passing true by position instead runs, but prints Bauer Legal: €6,200.00, 5 working days.

  • JavaScript has no keyword arguments. In a call, name = value is an assignment, an expression with a value, so it can sit where an argument goes.
  • At the bottom, rush was never declared, and an ES (ECMAScript) module runs in strict mode, where that’s an error, not a quiet new global.
  • Inside quoteLine(), rush is a parameter, so rush = rush assigns it to itself. Its value, true, is quote()’s second argument, rate: 6200 * true is 6200, and rush keeps its default, so five days. Python’s quote(6200, True) gives (6200, 5) too: what JavaScript lacks is the name.

When a call needs names, pass one object. Before each colon is the option’s name, written bare, as in Python’s rush=rush: it’s the name itself, never a variable. The function takes the object apart in its parameter list with a destructuring pattern:

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 In this lesson, you’ll pass options by name in JavaScript. Python’s quote line takes a rush flag and passes it on by name, and Bauer Legal’s call sets it by name too.

00:11 Run it: 1,116 euros, two working days.

00:18 The JavaScript port carries both keyword calls across: rush = rush inside, rush = true at the bottom. Run it, and JavaScript stops with a ReferenceError: rush is not defined.

00:33 The message is about a variable, not an argument. The quickest fix is to drop the name and pass true by position. Now the file runs. What does the line read?

00:47 Your turn: pause the video and predict.

00:55 6,200 euros, and five working days. Python prints 1,116 euros and two days.

01:07 JavaScript has no keyword arguments. In a call, name = value is an assignment, an expression with a value, so it can sit where an argument goes.

01:18 At the bottom, rush was never declared, and an ES module runs in strict mode, where that’s an error, not a quiet new global. Inside quote line, the parameter rush is assigned to itself.

01:33 The assignment’s value, true, is quote’s second argument, the rate. 6,200 times true is 6,200, and rush keeps its default, so five days.

01:47 Python does the same with True by position. What JavaScript lacks is the name.

01:55 So pass one object instead: each option’s name, as in Python’s rush=rush, then a colon and its value. Up in quote, the three options merge into one parameter, in braces: a destructuring pattern.

02:12 It takes the object apart by name, as fee and days take an array apart by position. Each equals sign gives a default, as in Python’s def.

02:24 Back in quote line, rush and format merge the same way, and the bottom call passes an object. Run it: 1,116 euros, two working days.

02:36 Options can come in any order and give the same quote, as in Python. But in JavaScript, only a function written to take an object has names.

02:48 That’s the JavaScript way: no keyword arguments, so pass one object and take it apart by name, with each default kept. Next up is the text lesson Making Every Option Optional.

03:01 Café Linde’s request ticks no options, so you’ll make the whole object optional, with a shorter way to pass rush on.

Become a Member to join the conversation.