Template Literals
Template Literals
π¨βπΌ Remember how tedious string concatenation was? Template literals make
combining strings and expressions much easier!
Instead of quotes, template literals use backticks:
console.log(`Hello, World!`)
What makes these so special is you don't have to escape quotes or even
newline characters:
console.log(`Hello,
World!`)
// Prints the same thing as: console.log('Hello,\nWorld!')
In addition, while you can still use string concatenation, you can embed any expression using
${expression}:console.log(`2 + 2 = ${2 + 2}`) // Prints: 2 + 2 = 4
console.log(`Hello, ${'World'}!`) // Prints: Hello, World!
π¨ Open
and complete the following tasks:
- Log a template literal whose output is exactly
The answer is 42, embedding a calculation that evaluates to42(do not hardcode the digits42as plain text alone) - Log a greeting template literal that includes both
HelloandTypeScript(for example:Hello, TypeScript!) - Log a math sentence whose output includes
50as the computed answer (for example:10 times 5 equals 50), embedding the multiplication in the template
Completion criteria
Your output should include:
The answer is 42- Text containing both
HelloandTypeScript(any casing is fine for the check) - The number
50as part of a logged math sentence
π° The backtick key is usually in the top-left of your keyboard, below Escape.