This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
The static String.raw() method is a tag function of template strings, like the r prefix in Python or the @ prefix in C# for string literals, this function is used to get the raw string form of template strings.
Syntax
String.raw(callSite, ...substitutions)
String.raw`templateString`
Parameters
-
callSite -
Well-formed template call site object, like
{raw: "string"}. - ...substitutions
- Contains substitution values.
- templateString
-
A template string, optionally with substitutions (
${...}).
Throws
-
TypeError -
A
TypeErroris thrown if the first argument is not a well formed object.
Description
In most cases String.raw is used with template strings. 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.
The String.raw() is the only built-in tag function of template strings, it works just like the default template function and performs concatenation, you can even re-implement it with normal JavaScript code.
Examples
String.raw`Hi\n${2+3}!`;
// "Hi\\n5!", the character after "Hi" is not a newline character,
// "\" and "n" are two characters.
String.raw`Hi\u000A!`;
// "Hi\\u000A!", same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective and
// backslashes will be present in the output string.
// You can confirm this by checking the .length property of the string.
let name = "Bob";
String.raw`Hi\n${name}!`;
// "Hi\\nBob!", substitutions are processed.
// Normally you would not call String.raw as a function, but you can do so:
String.raw({raw: "test"}, 0, 1, 2);
// "t0e1s2t"
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 6 (ECMA-262) The definition of 'String.raw' in that specification. |
Draft | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | Not supported | 34 (34) | Not supported | Not supported | Not supported |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | Not supported | Not supported | 34.0 (34) | Not supported | Not supported | Not supported |