Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

String.raw

String.raw() creates a string from a template literal without interpreting its escape sequences. Interpolated expressions are still evaluated and inserted.

How it works

String.raw is the built-in tag for producing a template literal's raw text. It inserts evaluated substitutions but leaves escape sequences such as \n unprocessed; it can also be called with an object containing a raw array-like property.

Syntax

String.raw(strings)
String.raw(strings, sub1)
String.raw(strings, sub1, sub2)
String.raw(strings, sub1, sub2, /* …, */ subN)
String.raw`templateString`

Parameters

NameTypeDescription
stringsobjectA template literal array-like object with a raw property containing the literal segments.
sub1, …, subNanyValues substituted between the raw literal segments; zero or more may be supplied.
templateStringtemplate literalA template literal, optionally containing substitutions, when using tag syntax.

Returns

The raw string form of the supplied template literal.

Key points

  • It is the standard built-in tag for reading template literal segments in raw form.
  • Expressions still evaluate, while backslash escape notation remains literal in the resulting string.
  • When called as an ordinary function, its first argument needs a raw property containing an array-like sequence of strings.
  • The trailing arguments supply the values placed between adjacent raw segments.
  • Its result is one string formed from raw segments and their substitutions.
  • The tagged-template form is the usual way to use this static method.

Common mistakes

  • A missing, zero, or negative raw length produces an empty string.

Example: Preserve an escape sequence

const user = "Ada";
const message = String.raw`Hello\n${user}!`;

console.log(message); // "Hello\\nAda!"

The substitution is evaluated, while the backslash and n remain two literal characters.

When to use it

Preserve backslashes

Constructing Windows-style paths or other text where backslashes must remain literal.

Keep escape notation

Generating source-like text that includes escape notation without first doubling every backslash.

Build raw interpolated text

Using a standard tag that combines literal segments with evaluated values while retaining raw segment text.

Interview questions

QWhat does String.raw do?

String.raw is a tag function for template literals. It returns the raw template text, so substitutions are processed but escape sequences are preserved as written.

Follow-up: How can it be called without tagged-template syntax? Pass an object such as { raw: ["a", "b"] } followed by the substitution values.

QWhat arguments does String.raw expect?

The first argument is a template-like object whose raw property is array-like and contains the literal segments. The remaining arguments provide substitution values inserted between those segments.

Follow-up: What happens when there are fewer substitutions than raw segments? Missing substitutions are treated as empty strings.

QWhat does String.raw return?

It returns the raw string form of the template literal. If the raw array-like object's length is non-positive, the result is the empty string.

Follow-up: What happens to substitution values that are not strings? They are converted to strings during concatenation.

  • String
  • Template literals
  • Tagged templates
  • String.raw()

Source: String.raw() - JavaScript - MDN Web Docs. Checked 2026-10-15.