JavaScript raw()
The .raw() static method is a tag function for template literals that returns the raw string form without processing escape sequences. It allows developers to access the literal characters in a template string, including backslashes and other escape characters, exactly as they were typed in the source code.
The .raw() method is particularly useful when working with file paths, regular expressions, or any scenario where escape sequences should be preserved rather than interpreted. It serves as the only built-in template literal tag function in JavaScript and provides functionality similar to raw string literals found in other programming languages.
Syntax
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 literal array object with arawproperty whose value is an array-like object of stringssub1,...,subN: Contains substitution values that replace placeholders in the template literal
Return value:
The .raw() method returns a string representing the raw string form of the given template literal.
Example 1: Basic Usage
This example demonstrates the fundamental difference between a regular template literal and String.raw():
// Regular template literal processes escape sequencesconst regularString = `Line 1\nLine 2\tTabbed`;console.log(regularString);// String.raw() preserves escape sequencesconst rawString = String.raw`Line 1\nLine 2\tTabbed`;console.log(rawString);console.log(rawString.length); // 20 characters including \n and \t
This example results in the following output:
Line 1Line 2 TabbedLine 1\nLine 2\tTabbed22
In this example, the regular template literal interprets \n as a newline character and \t as a tab character. However, String.raw() treats these as literal backslash-n and backslash-t character sequences, preserving them exactly as written.
Example 2: File Path Handling
This example shows how String.raw() simplifies working with Windows file paths that contain backslashes:
// Without String.raw() - requires double escapingconst windowsPath1 = 'C:\\Users\\John\\Documents\\file.txt';console.log(windowsPath1);// With String.raw() - no escaping neededconst windowsPath2 = String.raw`C:\Users\John\Documents\file.txt`;console.log(windowsPath2);
This example results in the following output:
C:\Users\John\Documents\file.txtC:\Users\John\Documents\file.txt
This example demonstrates how String.raw() eliminates the need for double-escaping backslashes when working with file paths, making the code more readable and less error-prone.
Codebyte Example: Regular Expression Patterns
This example illustrates using String.raw() to create cleaner regular expression patterns by avoiding double escaping. It compares traditional string-based regex patterns with those created using String.raw() for both date and URL matching:
Frequently Asked Questions
1. When should I use String.raw() instead of regular template literals?
Use String.raw() when you need to preserve escape sequences exactly as written, such as when working with file paths, regular expressions, or any text that contains backslashes that should not be interpreted as escape characters.
2. Can I use String.raw() with variable substitution?
Yes, String.raw() supports template literal substitution using ${} syntax. The substituted values are processed normally while escape sequences in the literal parts remain raw.
3. Does String.raw() work with all escape sequences?
Yes, String.raw() preserves all escape sequences including \n, \t, \", \', \\, and Unicode escapes like \u0041. None of these are processed or converted.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours