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

Building Strings With Template Literals

Building Strings With Template Literals

In this lesson, you ported the crew’s two f-string lines about Harbor Square to template literals, and found out what happens to a format spec on the way.

  • Where Python writes an f-string, JavaScript writes a template literal: the string goes in backticks, with no f in front, and each value goes in a ${...} placeholder.
  • A placeholder holds one expression, as an f-string’s braces do, such as ${name} or ${fill.toFixed(0)}. Unlike an f-string, it takes nothing after the expression.
  • Template literals have no format spec. A spec carried over from Python, as in ${fill:.0f}, stops the whole file with SyntaxError: Missing } in template expression, and none of the file runs.
  • To round a number, call .toFixed() with the number of decimals you want. fill.toFixed(0) gives the text 93. It does only the rounding part of a spec, and a tie, a number exactly halfway between two whole numbers, can round the other way: (2.5).toFixed(0) gives 3, where Python’s f"{2.5:.0f}" gives 2, because Python rounds a tie to the even one of the two and .toFixed() rounds it up. Number formatting comes later.
  • In ordinary quotes, ${name} is plain text, as {name} is in a Python string without the f: the backticks play the f’s role.

Here’s the crew’s Python script:

Language: Python
name = "Harbor Square"
bikes = 14
slots = 1
fill = bikes / (bikes + slots) * 100

print(f"{name}: {bikes} bikes")
print(f"{name}: {fill:.0f}% full")

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 crew’s Python script prints report lines like these about Harbor Square. In this lesson, you’ll build the same lines in JavaScript. Python builds each one with an f-string.

00:15 The second line works out the fill rate and formats it to a whole percent. Your JavaScript port already names the dock’s numbers, with a const for each one.

00:27 Start with the first line. In JavaScript, the string goes in backticks instead of quotes, with no f in front. Each value goes in a hole, which the docs call a placeholder: a dollar sign, then the braces you know from Python.

00:44 Run the port, and it prints the same line as Python. A string in backticks is called a template literal. Same as Python: in ordinary quotes, the dollar signs and braces stay plain text, as braces do when you leave off the f, because the backticks play the f’s role.

01:05 Now the fill rate. The fill line ports the way the others did, with the const keyword in front. Then the print line ports as it reads, with the format spec still inside the braces.

01:18 Python prints the fill rate as ninety-three. What will JavaScript print when you run the file? Your turn: pause the video and predict.

01:32 Nothing at all. The file stops with a syntax error: a missing closing brace.

01:40 Even the first line didn’t print. A syntax error means JavaScript couldn’t read the file, so none of it ran.

01:50 The caret points at fill. The error’s template expression is the code in a hole, and JavaScript reads fill as that expression. After it, JavaScript expects the closing brace, but it finds a colon.

02:05 A hole holds the expression and nothing after it: a template literal has no format spec.

02:13 Drop the spec, and the file runs, but it prints every digit.

02:19 The hole takes any expression, as an f-string’s braces do, so you round inside it. Call its .toFixed() method with a zero, the number of decimals you want.

02:30 It turns the fill rate into the text ninety-three. Run it again, and both lines print, as Python’s did. It does only the rounding part of a format spec, and a tie, a number exactly halfway between two whole numbers, can round the other way.

02:48 In JavaScript, two point five gives three; in Python, it gives two. Python rounds a tie to the even one of the two, and .toFixed() rounds it up.

03:01 So here’s the rule. Where Python writes an f-string, JavaScript writes a template literal, in backticks, with each value in a dollar-sign hole. The hole holds an expression and nothing after it, so you round the number with a method call inside it.

03:18 Number formatting comes later. Next up is the exercise Print a Dock’s Fill Rate. You’ll work out another dock’s fill rate and print it, rounded to a whole percent.

Become a Member to join the conversation.