JavaScript String raw() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Static method

What You’ll Learn

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

Introduction

In a normal template literal, `Hi\nthere` contains a real newline. With String.raw`Hi\nthere`, you get the characters H i \ n t h… — 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(...).

This page is part of JavaScript String Methods under Static Methods. Related topics include fromCharCode() and fromCodePoint().

Understanding the raw() Method

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.

📝 Syntax

Two ways to call the static String.raw method (from MDN):

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

String.raw`templateString`

Parameters

  • strings — a well-formed template array object like { raw: ['foo', 'bar'] } (provided automatically when used as a tag).
  • sub1 … subN — substitution values for ${...}.
  • templateString — a template literal, optionally with substitutions.

Return value

The raw string form of the template (substitutions applied, escapes not).

Exceptions

TypeError if the first argument has no usable raw property.

Common patterns

JavaScript
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!'  — backslash + n, then 5

const path = String.raw`C:\Development\profile\about.html`;

const name = "Bob";
String.raw`Hi\n${name}!`;  // 'Hi\\nBob!'

// Advanced call shape:
String.raw({ raw: "test" }, 0, 1, 2); // "t0e1s2t"

⚡ Quick Reference

GoalCode
Raw templateString.raw`Hi\n`
Windows pathString.raw`C:\Users\Sam`
With substitutionString.raw`Hi\n${name}`
RegExp sourcenew RegExp(String.raw`\d+\.\d+`)
True identity tag(s,...v)=>String.raw({raw:s},...v)
Normal template`Hi\n` (newline cooked)

🔍 At a Glance

Four facts to remember about String.raw().

Kind
static tag

Call on String

Escapes
raw

\n stays \ + n

${}
evaluated

Still interpolated

Best for
paths / regex

Fewer \\ doubles

📋 String.raw`...` vs normal `...`

String.raw`Hi\n``Hi\n`
Escape \nTwo chars: \ and nOne newline character
${expr}EvaluatedEvaluated
Typical usePaths, regex sourcesEveryday text / HTML
Length of Hi\n!54 (newline is one char)

Examples Gallery

Examples follow MDN String.raw() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See raw paths and how escapes stay visible.

Example 1 — Windows Path Without Double Escapes

MDN demo: write a path with single backslashes.

JavaScript
const filePath = String.raw`C:\Development\profile\about.html`;

console.log(`The file was uploaded from: ${filePath}`);
// The file was uploaded from: C:\Development\profile\about.html
Try It Yourself

How It Works

Without String.raw, a normal string would need "C:\\Development\\profile\\about.html". The tag keeps each \ as written.

Example 2 — Escapes Stay Raw

\n and \u000A are not turned into a newline.

JavaScript
JSON.stringify(String.raw`Hi\n${2 + 3}!`);
// "Hi\\n5!"

JSON.stringify(String.raw`Hi\u000A!`);
// "Hi\\u000A!"

String.raw`Hi\n!`.length;  // 5
Try It Yourself

How It Works

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}!"
Try It Yourself

How It Works

Escaping ${ with a backslash keeps a backslash in the output. Prefer a substitution like ${"$"} when you need a clean dollar sign.

Example 4 — Readable RegExp with String.raw

MDN pattern: build a RegExp without drowning in \\ and \/.

JavaScript
const re = new RegExp(
  String.raw`https://developer\.mozilla\.org/en-US/docs/Web/JavaScript/Reference/`
);

re.test(
  "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/"
); // true

function makeURLRegExp(path) {
  return new RegExp(String.raw`https://developer\.mozilla\.org/${path}`);
}

makeURLRegExp(".*").test("https://developer.mozilla.org/anything"); // true
Try It Yourself

How It Works

You write \. once for a literal dot. In a normal string you would need "\\.". Dynamic path pieces still use ${path}.

Example 5 — Identity Tag vs Naive String.raw

MDN warning: do not assign const html = String.raw if you want cooked escapes.

JavaScript
JSON.stringify(String.raw`\n`);
// "\\n"  — still raw

const html = (strings, ...values) =>
  String.raw({ raw: strings }, ...values);

JSON.stringify(html`\n`);
// "\n"  — newline cooked

String.raw({ raw: "test" }, 0, 1, 2);
// "t0e1s2t"
Try It Yourself

How It Works

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.

🚀 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.

📝 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.
  • Related static helpers: fromCharCode(), fromCodePoint().
  • Continue with the String constructor.

Browser & Runtime Support

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.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

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.

Continue with fromCodePoint(), fromCharCode(), String constructor, or the String methods hub.

💡 Best Practices

✅ Do

  • Use String.raw`...` for paths and regex sources
  • Rely on ${} for dynamic pieces
  • Use JSON.stringify when debugging escapes
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about String.raw()

Raw escapes, live substitutions, great for paths and regex.

5
Core concepts
🔢 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.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful