Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.raw — raw template literal segments

String.raw is a static method used as a template literal tag. It preserves the spelling of sequences such as \n in literal text segments, while still evaluating ${...} expressions and inserting their values.

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

String.raw`text${expression}text`
String.raw(template, ...substitutions)

String.raw is a static method, not a method of an individual string. As a template literal tag, it joins raw literal text segments with substitutions. A \n sequence in a literal segment produces a backslash and the letter n, rather than a newline character.

Expressions inside ${...} are still evaluated and their values converted to strings. Raw handling applies to literal segments, not substitutions: if a substituted value already contains a newline character, that character remains a newline. The method does not undo earlier escape processing and does not provide safe encoding for HTML or SQL.

An ordinary call, String.raw(template, ...substitutions), is also possible. The method reads template.raw as an array-like object and interleaves its elements with substitutions. Missing substitutions add nothing, extra substitutions are ignored, and a zero-length raw produces an empty string. A missing raw or a null value causes a TypeError.

In the example, the first line contains a literal \n, the second confirms the string contents, and the third demonstrates the omitted substitution between B and C.

Examples

const name = "Ada";
const text = String.raw`Hello\n${name}!`;
console.log(text);
console.log(text === "Hello\\nAda!");
console.log(String.raw({ raw: ["A", "B", "C"] }, 7));

Result

Hello\nAda!
true
A7BC

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