Preserve backslashes
Constructing Windows-style paths or other text where backslashes must remain literal.
JavaScript · Function
String.raw() creates a string from a template literal without interpreting its escape sequences. Interpolated expressions are still evaluated and inserted.
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.
String.raw(strings)
String.raw(strings, sub1)
String.raw(strings, sub1, sub2)
String.raw(strings, sub1, sub2, /* …, */ subN)
String.raw`templateString`| Name | Type | Description |
|---|---|---|
strings | object | A template literal array-like object with a raw property containing the literal segments. |
sub1, …, subN | any | Values substituted between the raw literal segments; zero or more may be supplied. |
templateString | template literal | A template literal, optionally containing substitutions, when using tag syntax. |
The raw string form of the supplied template literal.
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.
Constructing Windows-style paths or other text where backslashes must remain literal.
Generating source-like text that includes escape notation without first doubling every backslash.
Using a standard tag that combines literal segments with evaluated values while retaining raw segment text.
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.
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.
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.
StringTemplate literalsTagged templatesString.raw()