Sub topic 1
Writing Strings with Backticks
JavaScript has three ways to write a string literal: single quotes, double quotes, and backticks. A string written with backticks is called a template literal. This chapter covers what the backtick form is, how it compares to the quote forms, and the mistakes people often make when they first use it. Later chapters cover interpolation, multiline text, escaping, and tagged templates in detail.
Prerequisites
This chapter assumes you already know:
- JavaScript variables, expressions, and functions
- Regular string literals with single and double quotes, including escape sequences such as
\n - How to run JavaScript in Node.js or a browser console
The Basic Form
A template literal starts and ends with the backtick character (` `). On most US and UK keyboards, the backtick sits on the key to the left of 1`.
const single = 'Hello';
const double = "Hello";
const backtick = `Hello`;
console.log(typeof backtick); // string
console.log(single === double, double === backtick); // true trueOnce it is evaluated, a template literal is an ordinary string primitive. It has no special template type and no leftover link to the source code. Every string method works on it, and comparing it to a quoted string with the same characters gives true.
What Backticks Add
Single and double quotes behave the same, apart from which quote character needs escaping. Backticks add three things:
| Feature | `'...'` and `"..."` | `` `...` `` |
|---|---|---|
| Embed expressions with `${}` | No, the text appears literally | Yes |
| Real line breaks in the source | SyntaxError | Allowed, kept in the string |
| Use `'` and `"` without escaping | Only the other quote type | Both |
| Can be passed to a tag function | No | Yes |
| Standard escapes like `\n`, `\t`, `\u00e9` | Yes | Yes |
A small preview of the first feature, which the next chapter covers:
const user = "Ada";
console.log(`Welcome, ${user}`); // Welcome, AdaQuotes Inside Backticks
Since the backtick is the delimiter, both kinds of quote can appear inside a template literal without escaping. This helps with English prose and with snippets of HTML or SQL.
const quoted = `She said "it's fine" and left.`;
console.log(quoted); // She said "it's fine" and left.
const sameWithQuotes = "She said \"it's fine\" and left.";
console.log(quoted === sameWithQuotes); // trueYou do have to escape a backtick that appears inside a template literal. The chapter on escaping explains how.
Escape Sequences Still Work
Backslash escapes mean the same thing inside backticks as inside quotes:
const s = `Tab:\tEnd\nCaf\u00e9`;
console.log(s);
// Tab: End
// Café
console.log(s.length); // 13Common Errors
Using `${}` inside ordinary quotes
Only backticks support interpolation. Quoted strings keep ${...} as plain characters.
const name = "Ada";
console.log('Hi ${name}'); // Hi ${name}
console.log(`Hi ${name}`); // Hi AdaLinters such as ESLint flag this mistake with the no-template-curly-in-string rule.
Typing the wrong character
The backtick (` `, U+0060) is a different character from the apostrophe (') and the acute accent (´). Some international keyboard layouts treat the backtick as a dead key, which waits for the next keystroke before producing a character. Word processors and chat apps may also swap characters as you type. If you paste code and get SyntaxError: Invalid or unexpected token`, check which delimiter you actually have.
Forgetting the closing backtick
Quoted strings fail at the end of the line when the closing quote is missing. Template literals are allowed to span lines, so a missing closing backtick swallows the rest of the file, and the error appears far from the real mistake, often as Unterminated template literal.
Expecting backticks in JSON
JSON only allows double-quoted strings. ` JSON.parse('hi') ` throws. Template literals exist only in JavaScript source code.
Expecting a reusable template
A template literal is evaluated once, at the moment the code runs. It does not store a pattern to fill in later. If you need a reusable template, wrap the literal in a function:
const greet = (name) => `Hello, ${name}`;
console.log(greet("Ada")); // Hello, Ada
console.log(greet("Lin")); // Hello, LinWhen to Use Which
Many style guides use backticks only when you need one of their features and quotes everywhere else. Some teams use backticks for every string. Either rule works if you apply it consistently. Performance is not a reason to choose one: a template literal without substitutions compiles to the same string as a quoted one.
Exercise
What do these three lines print, and why? const n = 5; console.log('n is ${n}'); console.log(\n is ${n}\ === "n is 5");
Show answer
The first line prints "n is ${n}", because single-quoted strings do not interpolate, so the characters stay as written. The second line prints true, because the template literal evaluates to the ordinary string "n is 5", which is strictly equal to the quoted string.
Exercise
Rewrite "He said \"don't\" twice." using a template literal with no escapes, and state whether the two strings are equal.
Show answer
He said "don't" twice. needs no escapes, because neither quote character is the delimiter. The two strings are strictly equal (===), since both evaluate to the same sequence of characters.