String.raw() is a static tag function for template literals (same API as MDN String.raw()). Think of Python’s r"..." or C#’s @"...": ${} substitutions still run, but escapes like \n stay as text. Learn paths, regex patterns, identity-tag pitfalls, five examples, and try-it labs.
01
Kind
Static tag
02
Call as
String.raw`...`
03
Returns
String
04
Escapes
Kept raw
05
${}
Still evaluated
06
Baseline
Widely available
Fundamentals
Introduction
In a normal template literal, `Hi\nthere` contains a real newline. With String.raw`Hi\nthere`, you get the characters Hi\nth… — the backslash is preserved.
That makes Windows paths, regex sources, and other backslash-heavy text much easier to read. Substitutions still work: String.raw`Hi\n${name}!` inserts name, but keeps \n as two characters.
💡
Static tag function
Write String.raw`C:\Users\Sam` (backticks after raw). Do not write "text".raw(...).
Template literals have a cooked form (escapes processed) and a raw form (escapes kept as written). String.raw builds the result from the raw pieces, then inserts evaluated substitutions between them.
Usually used as a tag: String.raw`template`.
Can also be called as a function with { raw: [...] } (advanced).
It is the only built-in template tag in JavaScript.
Not a true “identity” tag — escapes stay raw unless you pass cooked strings on purpose.
Foundation
📝 Syntax
Two ways to call the static String.raw method (from MDN):
JSON.stringify shows the backslashes clearly. The ${2 + 3} substitution still becomes 5.
📈 Practical Patterns
Substitutions, RegExp sources, and identity-tag caveats.
Example 3 — Substitutions Still Run
Variables are interpolated; escape text stays literal.
JavaScript
const name = "Bob";
JSON.stringify(String.raw`Hi\n${name}!`);
// "Hi\\nBob!"
// Insert a literal ${name} using a substitution:
JSON.stringify(String.raw`Hi ${"$"}{name}!`);
// "Hi ${name}!"
Passing { raw: strings } pretends the cooked pieces are raw, so escapes process normally. The { raw: "test" } form treats a string as an array of characters.
Applications
🚀 Common Use Cases
File / Windows paths — avoid doubling every backslash.
RegExp constructors — readable patterns with dynamic pieces.
URLs and slash-heavy text — fewer escaping mistakes.
Debugging templates — inspect the raw form of escapes.
Custom tags — reuse String.raw({ raw: ... }) when building helpers.
Not for cooked HTML newlines — use a real identity tag if \n must become a break.
🧠 How String.raw`...` Works
1
Parse the template
Engine splits literal text and ${} expressions.
Parse
2
Keep raw literal pieces
Escape sequences stay as written in strings.raw.
Raw
3
Evaluate substitutions
Each ${expr} becomes a value (stringified when joined).
Subs
4
✅
Concatenate and return
Raw chunks + substitutions become one string.
Important
📝 Notes
Always use the tag form String.raw`...` for everyday work.
Substitutions run; escapes do not.
A trailing single \ before the closing backtick can break the template — use ${"\\"} for a final backslash.
Do not use bare String.raw as an HTML identity tag if you need cooked escapes.
String.raw() is Baseline Widely available — supported across modern browsers since ES2015.
✓ Baseline · Widely available
String.raw()
Safe for production. Ideal for paths and RegExp sources; remember substitutions still evaluate.
UniversalWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
String.raw()Excellent
Bottom line: Use String.raw`...` when you want readable backslashes. ${} still runs. Prefer a cooked identity tag when \n must become a real newline.
Wrap Up
Conclusion
String.raw() is JavaScript’s built-in raw template tag: keep escape characters as text, still interpolate ${} values, and write paths or regex patterns without double-escaping noise.
Build a cooked identity tag when tools need a named tag
Prefer substitutions for literal `, $, or trailing \
❌ Don’t
Call raw on a string instance
Expect \n inside String.raw to become a newline
Assign const html = String.raw and assume cooked escapes
End a raw template with a lone \ before the closing backtick
Forget that ${} still evaluates expressions
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.raw()
Raw escapes, live substitutions, great for paths and regex.
5
Core concepts
📄01
Static tag
String.raw`...`
API
🔢02
Escapes
kept raw
Core
⚡03
${}
still runs
Subs
📁04
Paths
single \
Win
⚠️05
Identity
use cooked
Caveat
❓ Frequently Asked Questions
String.raw is a static tag function for template literals. Substitutions like ${expr} still run, but escape sequences such as \n stay as the characters backslash and n instead of becoming a newline.
No. It is a static method on the String constructor. Use it as a tag: String.raw`C:\path\file.txt`, not "text".raw(...).
Yes. Expressions inside ${...} are evaluated and inserted. Only escape sequences in the literal text are left raw.
Windows/file paths, regex patterns with many backslashes, and any template where you want readable \ sequences without double-escaping in a normal string.
Not safely. String.raw keeps escapes raw, so \n will not become a newline. For a true identity tag, pass the cooked strings: (s, ...v) => String.raw({ raw: s }, ...v).
You still get a string built from the raw template text. Calling String.raw with a malformed first argument (missing raw) throws TypeError.
Did you know?
String.raw is the only template tag built into the language. Every other tag (like html`...` in libraries) is a normal function you write yourself.