Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
The String.raw() static method is a tag function of template literals. This is similar to the r prefix in Python, or the @ prefix in C# for string literals. It's used to get the raw string form of template literals — that is, substitutions (e.g., ${foo}) are processed, but escape sequences (e.g., \n) are not.
Well-formed template literal array object, like { raw: ['foo', 'bar', 'baz'] }. Should be an object with a raw property whose value is an array-like object of strings.
sub1, …, subNContains substitution values.
templateStringA template literal, optionally with substitutions (${...}).
The raw string form of a given template literal.
Thrown if the first argument doesn't have a raw property, or the raw property is undefined or null.
In most cases, String.raw() is used with template literals. The first syntax mentioned above is only rarely used, because the JavaScript engine will call this with proper arguments for you, (just like with other tag functions).
String.raw() is the only built-in template literal tag. It has close semantics to an untagged literal since it concatenates all arguments and returns a string. You can even re-implement it with normal JavaScript code.
Warning: You should not use String.raw directly as an "identity" tag. See Building an identity tag for how to implement this.
If String.raw() is called with an object whose raw property doesn't have a length property or a non-positive length, it returns an empty string "". If substitutions.length < strings.raw.length - 1 (i.e., there are not enough substitutions to fill the placeholders — which can't happen in a well-formed tagged template literal), the rest of the placeholders are filled with empty strings.
String.raw is a function, so it cannot circumvent basic template literal syntax such as backticks as delimiters and ${ for substitutions. If you want to include these characters in the output string, you need to escape them with backslashes. However, since String.raw outputs raw strings, the backslashes will be preserved in the output.
To work around this, you can use a substitution to insert these characters.
This approach works for String.raw because it just concatenates the raw strings and the substitutions. In general, unfortunately, there's no way for a template literal tag to receive a raw string that contains unescaped template literal syntax.
Combining a String.raw template literal with the RegExp() constructor allows you to create regular expressions with dynamic parts (which is not possible with regex literals) without double-escaping (\\) regular expression escape sequences (which is not possible with normal string literals). This is also valuable in strings that contain a lot of slashes, such as file paths or URLs.
Many tools give special treatment to literals tagged by a particular name.
One might naïvely implement the html tag as:
This, in fact, works for the case above. However, because String.raw would concatenate the raw string literals instead of the "cooked" ones, escape sequences would not be processed.
This may not be what you want for a "true identity" tag, where the tag is purely for markup and doesn't change the literal's value. In this case, you can create a custom tag and pass the "cooked" (i.e., escape sequences are processed) literal array to String.raw, pretending they are raw strings.
Notice the first argument is an object with a raw property, whose value is an array-like object (with a length property and integer indexes) representing the separated strings in the template literal. The rest of the arguments are the substitutions. Since the raw value can be any array-like object, it can even be a string! For example, 'test' is treated as ['t', 'e', 's', 't']. The following is equivalent to `t${0}e${1}s${2}t`:
| ECMAScript® 2027 Language Specification # sec-string.raw |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Nov 12, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.