Building Strings With Quotes and Backticks

The ticket_branch.py helper uses f-strings for every line it prints, so porting those strings looks like copying. One f-string habit carried into JavaScript breaks the branch name without raising an error.

Port the Branch Line

Single and double quotes make the same kind of string, as in Python, with the everyday escapes such as \n and \". In this course, you’ll use double quotes.

Values in text are where the languages differ. JavaScript’s equivalent of an f-string is the template literal, a string in backticks whose holes sit where an f-string has braces. For now, take key.toLowerCase() as key.lower(). Here’s the branch line, with the slug, the title’s first six words in lowercase joined by hyphens, written out by hand:

Language: JavaScript
const key = "PAY-2291";
const slug = "refund-emails-show-the-wrong-currency";
console.log(`Branch: fix/{key.toLowerCase()}-{slug}`);

Python’s f"Branch: fix/{key.lower()}-{slug}" fills in both holes. Predict what JavaScript does, then run it:

Language: Program Output
Branch: fix/{key.toLowerCase()}-{slug}

The backticks mark the string as a template, as the f does, but JavaScript also marks each hole. Only ${ opens one, so braces alone are ordinary characters, as doubled braces are in an f-string. Add the dollar signs:

Language: JavaScript
const key = "PAY-2291";
const slug = "refund-emails-show-the-wrong-currency";
console.log(`Branch: fix/${key.toLowerCase()}-${slug}`);

Now the branch matches the Python helper’s:

Language: Program Output
Branch: fix/pay-2291-refund-emails-show-the-wrong-currency

In JavaScript, you use double quotes for fixed text and switch to backticks as soon as a value goes in, with ${} around every hole.

Map the Rest of Your F-String Habits

The helper’s commit message runs over several lines. Template literals span lines as Python’s triple-quoted strings do, and they keep the line breaks and any indentation exactly as you type them:

Language: JavaScript
const key = "PAY-2304";
const note = `Ticket: ${key}
Status: open`;
console.log(note);

The string keeps its line break, and the hole still fills in:

Language: Program Output
Ticket: PAY-2304
Status: open

Here are the JavaScript equivalents of two more Python expressions:

Python JavaScript
f"{price:.2f}" `${price.toFixed(2)}`
str(2291) String(2291)

Watch for these slips as you port:

  • JavaScript has no string prefixes, so console.log(f"Branch: fix/{key}") stops with SyntaxError: missing ) after argument list.
  • Only backticks make a template literal, so "${key}" in ordinary quotes prints those six characters.
  • """Back at 9""" stops with SyntaxError: Unexpected string. Unlike a plain triple-quoted string, a backtick string still fills in any ${} it holds.
  • A hole holds only an expression, with no format mini-language, so you format the value yourself, as price.toFixed(2) does in the row above. It rounds a halfway value such as 1.125 up to 1.13, where Python prints 1.12.

A hole converts a value much as String() does, which spells Booleans the JavaScript way. So the helper’s reviewer line, with a fixed flag value for now, prints true where Python prints True:

Language: JavaScript
const needsReviewer = true;
console.log(`Needs a second reviewer: ${needsReviewer}`);

It’s the one printed line the port leaves different, and the course accepts it:

Language: Program Output
Needs a second reviewer: true

As in Python, a value in a hole needs no conversion call. + also joins text and a number without Python’s TypeError, but a template literal is the JavaScript way to mix them.

Next up is the exercise Write the Commit Message. Given the ticket’s key, its title, and the minutes logged against it, you’ll print the full commit message the review bot expects, exactly as the Python helper prints it.

Become a Member to join the conversation.