Which symbol is used to define a template literal in JavaScript?
Analysis & Theory
Template literals are enclosed in backticks (`), allowing embedded expressions and multiline strings.
How do you insert a variable into a template literal?
Analysis & Theory
Use `${variable}` to interpolate a variable inside a template literal.
What will this code return? `let name = 'Sam'; `Hello, ${name}!`;`
Analysis & Theory
Template literals evaluate `${name}` to 'Sam', resulting in `Hello, Sam!`.
What is a key benefit of template literals?
B
Multiline strings and embedded expressions
D
Strict mode enforcement
Analysis & Theory
Template literals support multiline strings and embedded expressions using `${}`.
Which of the following is a valid multiline string using template literals?
Analysis & Theory
Backticks (``) allow you to write multiline strings directly without escape characters.
What will this output? `let a = 5; let b = 10; console.log(`Sum: ${a + b}`);`
Analysis & Theory
The expression `${a + b}` is evaluated, so it prints `Sum: 15`.
Can you use template literals inside HTML templates?
B
Yes, with backticks and JS expressions
C
Only with external libraries
Analysis & Theory
Yes, template literals are often used in JavaScript to create dynamic HTML strings.
What will this return? ``console.log(typeof `Hello`)``
Analysis & Theory
Template literals are still strings, so `typeof` returns `'string'`.
Which of the following is NOT allowed in a template literal?
C
Function calls inside `${}`
D
Using single quotes for interpolation
Analysis & Theory
Interpolation must be inside `${}` — not with single quotes or other syntax.
What is the output of: `console.log(`The result is: ${10 * 2}`);`
A
The result is: ${10 * 2}
Analysis & Theory
`10 * 2` is evaluated inside `${}` and prints `The result is: 20`.