Template literals are string literals allowing embedded expressions. They use backticks (`) instead of quotes and can include placeholders with ${expression}.
Click to reveal answer
beginner
How do you embed a variable inside a template literal?
Use the syntax ${variableName} inside backticks. For example: `Hello, ${name}!` will insert the value of name.
Click to reveal answer
beginner
What advantage do template literals have over regular strings?
They allow multi-line strings without special characters and easy insertion of variables or expressions directly inside the string.
Click to reveal answer
beginner
Show an example of a multi-line string using template literals.
Hint: Expressions inside ${} are calculated before output [OK]
Common Mistakes:
Treating ${} as plain text
Not evaluating expressions inside ${}
Confusing string concatenation with template literals
4. Find the error in the following code snippet:
const user = "Bob";
console.log('Hello, ${user}!');
medium
A. Using single quotes instead of backticks for template literal
B. Missing semicolon after console.log
C. Variable user is not declared
D. Incorrect use of console.log function
Solution
Step 1: Check string delimiters for template literals
Template literals require backticks (`), but single quotes are used here.
Step 2: Understand effect of wrong quotes
Using single quotes treats ${user} as plain text, not variable substitution.
Final Answer:
Using single quotes instead of backticks for template literal -> Option A
Quick Check:
Backticks needed for ${} substitution [OK]
Hint: Use backticks, not quotes, for variable substitution [OK]
Common Mistakes:
Using single or double quotes instead of backticks
Expecting ${} to work inside normal quotes
Ignoring string delimiter rules
5. You want to create a multi-line string that includes a user's name and age using template literals. Which code correctly achieves this?
const name = "Eva";
const age = 28;
// Choose the correct code to print:
// Hello Eva,
// You are 28 years old.
hard
A. console.log(`Hello ${name},\nYou are ${age} years old.`);
B. console.log(`Hello ${name},
You are ${age} years old.`);
C. console.log('Hello ${name},\nYou are ${age} years old.');
D. console.log("Hello ${name},\nYou are ${age} years old.");
Solution
Step 1: Identify correct use of template literals and multi-line strings
Template literals allow multi-line strings directly without \n escape sequences.
Step 2: Check each option
console.log(`Hello ${name},
You are ${age} years old.`); uses backticks and writes the string on two lines, correctly showing multi-line output with variable substitution.
Final Answer:
console.log(`Hello ${name},
You are ${age} years old.`); -> Option B
Quick Check:
Backticks allow multi-line strings and ${} substitution [OK]
Hint: Use backticks and press Enter for multi-line strings [OK]
Common Mistakes:
Using quotes instead of backticks for multi-line
Using \n inside template literals unnecessarily
Not substituting variables inside quotes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.