0
0
Javascriptprogramming~5 mins

Template literals in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are template literals in JavaScript?
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.
Example:
const message = `Hello
World!`;
console.log(message);
This prints:
Hello
World!
Click to reveal answer
intermediate
Can you include expressions inside template literals? Give an example.
Yes. You can include any JavaScript expression inside ${}. Example:
const sum = `${2 + 3}`; prints '5'.
Click to reveal answer
Which symbol is used to start and end a template literal?
A" (double quote)
B` (backtick)
C' (single quote)
D~ (tilde)
How do you insert a variable named 'user' inside a template literal?
A${user}
B#user#
C$user
D{user}
What will this code output? `const a = 5; console.log(`Value is ${a * 2}`);`
AValue is 10
BValue is a * 2
CValue is ${a * 2}
DError
Which of these is a benefit of template literals?
AThey only work with numbers
BThey are slower than regular strings
CThey require escaping quotes
DThey allow multi-line strings easily
What will this print? `console.log(`Hello\nWorld`);`
AHello World
BHello\nWorld
CHello (new line) World
DError
Explain how to use template literals to create a greeting message including a person's name and age.
Think about how to write a sentence like 'Hello, Alice! You are 30 years old.' using variables.
You got /3 concepts.
    Describe the advantages of template literals compared to traditional string concatenation.
    Consider how template literals make code easier to read and write.
    You got /4 concepts.