Skip to the solution
EasyFrontend5 min read

What are template literals?

1.2k views

The problem

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.stringify showing 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.

  • template-literals

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: template literal interpolation of any expression, nesting, and a genuinely preserved literal newline

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 1 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

How would you use Object.groupBy() to group an array of orders by status, and how does it compare to a manual reduce()?

Continue