JavaScript String.raw() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.raw is a static tag for template literals. ${} substitutions still run, but escapes like \n stay as backslash + letter. Use it as String.raw`...` — not on a string instance.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call String.raw. Start with this line.

JavaScript
String.raw`...${expr}...`
CallWhat it does
String.raw`...${expr}...`Primary signature.
String.raw(...)Call with the arguments your case needs.

Built into JavaScript. Call String.raw(...) on the String constructor — no import required.

Library declarations
JavaScript
String.raw`...${expr}...`

Parameters

ParameterDescription
Template literalRequired. Text + optional ${} parts
{ raw: [...] } formOptional. Call style for custom tag helpers

Return Value

Return ValueDescription
*The value returned by String.raw().

JavaScript Examples

Example 1 — Windows Path Without Double Escapes

Write a path with single backslashes — no \\\\ noise.

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

  1. Tag the template. String.raw sits before the backticks.
  2. Keep each backslash. \D, \p, and \a stay as written — not escapes.
  3. Compare to a normal string. Without raw you would need "C:\\Development\\...".
  4. Log the path. The outer template only interpolates filePath; the path itself is already raw.

Example 2 — Escapes Stay Raw; ${} Still Runs

\n is two characters; the expression still becomes 5.

JavaScript
console.log(JSON.stringify(String.raw`Hi\n${2 + 3}!`)); // "Hi\\n5!"
console.log(String.raw`Hi\n!`.length);                   // 5
Try It Yourself

How It Works

  1. Raw literal text. \n is backslash + n, not a newline.
  2. Run the expression. ${2 + 3} inserts 5 as usual.
  3. Inspect with JSON. JSON.stringify shows the backslash clearly as \\n.
  4. Count characters. Hi\n! has length 5 — H, i, \, n, !.

Example 3 — String.raw vs a Normal Template

Same source text, different escape rules.

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

const re = new RegExp(String.raw`\d+\.\d+`);
console.log(re.test("3.14")); // true
Try It Yourself

How It Works

  1. Raw keeps escapes. First line still contains \ and n.
  2. Cooked processes them. A normal template turns \n into a newline character.
  3. RegExp benefit. Write \d and \. once — no "\\d+\\.\\d+" doubling.
  4. Pick by intent. Want a real newline? Skip String.raw. Want literal backslashes? Use it.

Browser & Runtime Support

String.raw is part of ES2015 and is Baseline Widely available across modern browsers.

✓ Baseline · ES2015

String.raw()

Supported in Chrome 41+, Firefox 34+, Safari 10+, Edge 12+, and Node.js 4+. Not available in Internet Explorer.

98% Modern browser support
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: Safe for modern web apps and current Node LTS releases. Feature-detect with if (typeof String.raw === "function") for very old targets.

Frequently Asked Questions

String.raw is a static tag function for template literals. Substitutions like ${expr} still run, but escape sequences such as \ 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.
Not safely. String.raw keeps escapes raw, so \ will not become a newline. For cooked escapes, use (s, ...v) => String.raw({ raw: s }, ...v).
Calling String.raw with a first argument that has no raw property throws TypeError.

Next: repeat()

Learn how to repeat a string a given number of times.

repeat() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

11 people found this page helpful