Question presented to candidate: "What are template literals, and what can they do that regular quoted strings genuinely can't?"
What a strong answer should cover:
- 📌 Interview term: template literals — strings delimited with backticks (
`) instead of single/double quotes, supporting interpolation (${expression}), genuine multiline text, and nesting. - 📌 Interview term: interpolation — any valid JavaScript expression inside
${...}is evaluated and converted to a string, directly embedded in place — verified directly with both a simple variable and an arithmetic expression evaluated inline. - 📌 Interview term: the real, direct answer to what quoted strings can't do — verified directly: a template literal with an actual line break inside its backticks genuinely produces a real newline character in the resulting string (confirmed via
JSON.stringifyshowing a literal\n) — a regular quoted string cannot contain a literal line break at all without an explicit escape sequence or string concatenation. - A precise answer names that template literals can be nested inside each other's
${...}interpolation — verified directly, a template literal genuinely evaluated correctly inside another template literal's interpolation. - A precise answer names tagged template literals (covered in more depth in this bank's own dedicated question) as a related, more advanced capability unique to template literal syntax — a function placed directly before the backticks can intercept and transform the literal's pieces, something no quoted string offers any equivalent for.
Clarifying questions expected:
- None — this is a definitional/technical question; naming the genuine, real capability gap (multiline + interpolation + tagging) versus just "nicer syntax" is the strong signal.
Code / implementation expected: Optional — showing real interpolation and a real preserved newline demonstrates concrete understanding beyond reciting the backtick syntax.