Challenge - 5 Problems
Template Literals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember that template literals preserve new lines inside backticks.
✗ Incorrect
The string contains a literal backslash followed by n, so console.log prints Hello\nWorld! as is.
❓ Predict Output
intermediate2: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}`);
Attempts:
2 left
💡 Hint
Expressions inside ${} are evaluated before being inserted.
✗ Incorrect
The expression a + b equals 15, so the output is 'Sum is 15'.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Look at how strings and values arrays are combined in the tag function.
✗ Incorrect
strings array is ['Value: ', ' and ', ''], values are [3,4]. The function returns strings[0] + doubled values joined + strings[1]. So 'Value: ' + '6' + '8' + ' and ' results in 'Value: 68 and'.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Template literals can be nested and inner ones are evaluated first.
✗ Incorrect
The inner template `Ms. ${name}` evaluates to 'Ms. Alice', then the outer template inserts that string.
🧠 Conceptual
expert2:00remaining
Why use tagged template literals?
Which of the following is a key reason to use tagged template literals in JavaScript?
Attempts:
2 left
💡 Hint
Tagged templates can process and transform template literal parts before output.
✗ Incorrect
Tagged template literals allow custom processing of template strings, such as escaping HTML to prevent injection attacks.