★ wanayoo — archive 1999 https://developer.mozilla.org/id/docs/Web/JavaScript/Reference/Global_Objects/String/rawNouvelle recherche | Portail wanayoo

String.raw()

Our volunteers haven't translated this article into Bahasa Indonesia yet. Join us and help get the job done!

This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.

The static String.raw() method is a tag function of template strings, similar to the r prefix in Python or the @ prefix in C# for string literals. It's used to get the raw string form of template strings (that is, the original, uninterpreted text).

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 (${...}).

Errors thrown

TypeError
A TypeError is 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.

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

Using String.raw()

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 2015 (6th Edition, ECMA-262)
The definition of 'String.raw' in that specification.
Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 41 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 41 34.0 (34) Not supported Not supported Not supported

See also

Document Tags and Contributors

Contributors to this page: Sheppy, fscholz, awalGarg, Mingun, ziyunfei
Last updated by: Sheppy,
Hide Sidebar