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

In this lesson, you saw what a Python keyword call does when it’s ported to JavaScript, and passed options by name the JavaScript way, with one object.

Python’s quote_line() passes rush on by name, quote(words, rush=rush), and its caller writes quote_line("Bauer Legal", 6200, rush=True). Here’s the translator’s Python script:

Language: Python
def quote(
    words,
    rate=0.12,
    rush=False,
    minimum=40,
):
    fee = max(words * rate, minimum)
    days = 2 + words // 2000
    if rush:
        return fee * 1.5, max(1, days // 2)
    return fee, days

def for_email(amount):
    return f"€{amount:,.2f}"

def quote_line(
    client,
    words,
    rush=False,
    fmt=for_email,
):
    fee, days = quote(words, rush=rush)
    return f"{client}: {fmt(fee)}, {days} working days"

print(quote_line("Bauer Legal", 6200, rush=True))

The script prints Bauer Legal: €1,116.00, 2 working days. The line-by-line port keeps both keyword calls:

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 On the quote form, a client can tick rush, and the backend has to pass that choice on. In this lesson, you’ll pass options by name in JavaScript.

00:13 In the Python script, quoteLine() takes a rush flag and passes it on to quote() by name. The caller sets it by name too, for Bauer Legal’s rush job.

00:25 Run it, and the line reads 1,116 euros, two working days.

00:32 The JavaScript port carries each line across. The two calls keep their names too: rush = rush inside, and rush = true at the bottom.

00:43 Run it, and JavaScript stops at the last line with a reference error: rush is not defined. The trace points at rush = true. And the message isn’t about an argument at all: it’s about a variable called rush.

01:00 The quickest fix for that message is to drop the name and pass true by position, as quoteLine()’s third argument. This time the file runs.

01:10 What does the line read? Your turn: pause the video and predict.

01:19 6,200 euros, and five working days. That’s one euro a word, and no rush. Python prints 1,116 euros and two days.

01:35 One rule explains both calls: JavaScript has no keyword arguments. In a call, name = value is an assignment. And an assignment is an expression with a value, the one it assigns, so it can sit where an argument goes.

01:52 At the bottom, rush = true hit a rush nobody declared. An ES module like this file always runs in strict mode, where that’s an error, not a quiet new global.

02:05 Inside quoteLine(), rush is a parameter, so rush = rush assigns it to itself, with no error.

02:14 The assignment’s value, true, is quote()’s second argument, so it fills the second parameter, the rate per word. The fee is 6,200 times true, which is 6,200: true counts as one, as it does in Python.

02:31 rush keeps its default of false, so the turnaround stays at five days.

02:36 Python would do the same with true by position. What JavaScript lacks is the name.

02:44 When a call needs names, a JavaScript caller passes one object. So the call to quote() gets an object literal. Before the colon is the option’s name, a key like Python’s quoted rush key, without the quotes.

03:00 After the colon is the value, here quoteLine()’s own flag.

03:06 Up in quote(), the three options merge into one parameter, wrapped in braces. That’s a destructuring pattern. It takes the object apart by name.

03:18 Array destructuring, as in fee and days, takes things apart by position. Each equals sign here gives a default, as in Python’s def, for an option the object leaves out.

03:30 In the caller’s object, a colon gives the value. Inside, rate, rush, and minimum are plain variables, so the body doesn’t change.

03:42 Back in quoteLine(), its own rush and format merge into a pattern the same way. The caller at the bottom passes rush true in an object.

03:52 Run it: no reference error, and the line reads 1,116 euros, two working days. Options can come in any order: rush then minimum, or minimum then rush, gives the same quote.

04:07 In Python, a def’s parameters can be passed by name that way. In JavaScript, only a function written to take an object has names.

04:18 That’s the JavaScript way: there are no keyword arguments. When a call needs names, pass one object, and take it apart by name in the parameter list, with each default kept.

04:31 Next up is Making Every Option Optional, a text lesson. Café Linde’s request ticks no options at all, and you’ll make the whole object optional, with a shorter way to pass rush on.

Become a Member to join the conversation.