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

Receiving Template Parts Before Combining Them

Receiving Template Parts Before Combining Them

In this lesson, the settings audit needs to keep each value as a value. The settings are flat, and level is the number 2.

In Python, the audit helper takes one string, built with an f-string. Python fills it in before the call:

Language: Python
def audit(text):
    print("audit:", text)

level = 2
audit(f"level={level}")

So the log shows audit: level=2, and the 2 is now plain text:

Language: Program Output
audit: level=2

Template literals work like f-strings, minus format specs such as :.2f, with a dollar sign before each pair of braces. Called with parentheses, audit() gets the combined string, as in Python. Put its name right before the backtick instead, and JavaScript calls it with the template itself: a tagged template, with audit as its tag.

To keep the 2 a value, the port makes audit the tag but keeps Python’s one text parameter and logs it. level is a constant, and the last line tags the template:

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, the settings audit needs to keep each value as a value. The settings are flat, and level is the number 2. In Python, the audit helper takes one string, built with an f-string.

00:14 Python fills it in before the call, so the log shows audit, level=2: the 2 is now plain text.

00:24 Template literals work like f-strings, with a dollar sign before each pair of braces. Called with parentheses, audit gets the combined string, as in Python.

00:35 Put its name right before the backtick instead, and JavaScript calls it with the template itself: a tagged template, with audit as its tag. To keep the 2 a value, the port makes audit the tag but keeps Python’s one text parameter, logged. level is a constant, and the last line tags the template.

00:57 Python’s helper received level=2. audit’s one parameter is text: what does it get, and where’s the 2? Your turn: pause the video and predict.

01:13 It logs audit, then an array: the text level=, and an empty string. The 2 isn’t there at all.

01:24 JavaScript never combines a tagged template. The tag’s first argument is an array of the literal parts, the text around each substitution. Each value comes after it, as an argument of its own.

01:37 So text got only the parts. The 2 had no parameter: Python would raise a TypeError, but JavaScript ignores extra arguments silently. The fix: the signature gains a rest parameter, like *values in Python, but it gathers an array, not a tuple.

01:57 Now the log shows both. There’s always one more part than values: here, the empty text at the end.

02:06 A tag is an ordinary function, so it can return anything. Here, audit returns a record: the parts, spread into a plain array it owns, and values, shorthand for values: values.

02:20 The one passed in is frozen, with a hidden extra property.

02:25 The log shows parts and values, side by side, never combined. And the 2 is still a number: no quotes around it, unlike the parts.

02:36 If you’ve used Python 3.14’s t-strings, they keep the parts apart too, but you call the function yourself, while a tagged template calls its tag.

02:47 That passed-in array has an extra property: raw. Each part comes twice. Cooked, in the array itself: escapes are processed, so backslash n is a real newline.

03:00 Raw, in strings.raw: the text as typed, a backslash and an n.

03:07 A small show tag logs both arrays for line, backslash n, next. Inside an array, console.log() writes strings the way you’d type them, like Python’s repr() in a list.

03:19 So the newline prints as backslash n, and the real backslash shows up doubled.

03:26 JavaScript ships one tag of its own: String.raw, which builds the string from the raw parts. Logged plain, the template breaks into two lines.

03:37 Through String.raw, backslash n stays on one line. That’s Python’s raw string. The analogy stops at the syntax: r is part of Python’s literal, while String.raw is a function the template calls, like audit.

03:56 So a tag isn’t an f-string with a name in front: nothing is combined unless the tag does it. And it isn’t automatic safety: a tag sees the values apart from the text, so its code could escape them for HTML.

04:11 But tags don’t escape anything automatically.

04:15 So in JavaScript, a tag receives the template uncombined: the parts first, then each value. Collect the values with a rest parameter, return the record you need, and use strings.raw or String.raw when escapes should stay as typed.

04:32 Next up is the exercise Keep Audit Values Structured. There, you’ll make an audit template with two values come back as one event per value, like level with its 2.

Become a Member to join the conversation.