0
0
Javascriptprogramming~20 mins

Template literals in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Literals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiline template literal
What is the output of this code?
const message = `Hello\nWorld!`;
console.log(message);
Javascript
const message = `Hello\nWorld!`;
console.log(message);
AHello\nWorld!
B
Hello
World!
CHello World!
DHello\n World!
Attempts:
2 left
💡 Hint
Remember that template literals preserve new lines inside backticks.
Predict Output
intermediate
2:00remaining
Embedding expressions in template literals
What will this code output?
const a = 5;
const b = 10;
console.log(`Sum is ${a + b}`);
Javascript
const a = 5;
const b = 10;
console.log(`Sum is ${a + b}`);
ASum is a + b
BSum is ${a + b}
CSum is 15
DSum is 510
Attempts:
2 left
💡 Hint
Expressions inside ${} are evaluated before being inserted.
Predict Output
advanced
2:00remaining
Tagged template literal output
What is the output of this code?
function tag(strings, ...values) {
  return strings[0] + values.map(v => v * 2).join('') + strings[1];
}
const result = tag`Value: ${3} and ${4}`;
console.log(result);
Javascript
function tag(strings, ...values) {
  return strings[0] + values.map(v => v * 2).join('') + strings[1];
}
const result = tag`Value: ${3} and ${4}`;
console.log(result);
AValue: 68 and
BValue: 6 and 8
Cdna 86 :eulaV
DValue: 34 and 8
Attempts:
2 left
💡 Hint
Look at how strings and values arrays are combined in the tag function.
Predict Output
advanced
2:00remaining
Nested template literals output
What will this code print?
const name = 'Alice';
const greeting = `Hello, ${`Ms. ${name}`}`;
console.log(greeting);
Javascript
const name = 'Alice';
const greeting = `Hello, ${`Ms. ${name}`}`;
console.log(greeting);
AHello, Ms. ${name}
BHello, Ms. Alice
CHello, Ms. Alice Alice
DHello, ${`Ms. ${name}`}
Attempts:
2 left
💡 Hint
Template literals can be nested and inner ones are evaluated first.
🧠 Conceptual
expert
2:00remaining
Why use tagged template literals?
Which of the following is a key reason to use tagged template literals in JavaScript?
ATo define variables inside template literals
BTo create multi-line strings without backticks
CTo evaluate expressions inside strings without ${}
DTo automatically escape HTML special characters in user input
Attempts:
2 left
💡 Hint
Tagged templates can process and transform template literal parts before output.