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.