Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Template literal

A template literal uses backticks (`) as delimiters. It supports embedded expressions through ${expression} and multiline text. A tagged variant passes text segments and expression values to a tag function.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

`text ${expression} text`
tag`text ${expression} text`

A template literal is delimited by backticks rather than single or double quotes. In its ordinary form, it produces a string. The ${expression} syntax evaluates an expression, converts its result to text, and inserts it at that position; substitutions are processed from left to right.

In an ordinary template literal, indentation and unescaped line breaks become part of the resulting string. A backslash immediately before a physical line break forms a line continuation and adds no newline to the string. Escape sequences such as \n are interpreted. To write a literal ${, use \${; prefix a backtick with a backslash. In ordinary strings delimited by ' or ", ${expression} does not perform interpolation.

Placing tag directly before the literal calls a tag function: it receives an array of processed text segments, followed by substitution values without automatic string conversion. The array's raw property exposes the unprocessed segments. The result is whatever the function returns, not necessarily a string.

The example combines interpolation, a calculation, and a line break. A template literal itself does not automatically provide HTML escaping or safe SQL query construction; appropriate processing must be supplied separately.

Examples

const name = "Ada";
const count = 3;
const message = `Hello, ${name}!
Total: ${count * 2}`;
console.log(message);

Result

Hello, Ada!
Total: 6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources