Relawan kami belum menerjemahkan artikel ini ke bahasa Bahasa Indonesia. Bergabunglah dan bantu kami menyelesaikannya!
Anda juga dapat membaca artikel ini dalam English (US).

The static String.raw() method is a tag function of template literals, similar to the r prefix in Python or the @ prefix in C# for string literals (yet there is a difference: see explanations in this issue). It's used to get the raw string form of template strings, that is, substitutions (e.g. ${foo}) are processed, but escapes (e.g. \n) are not.

Syntax

String.raw(callSite, ...substitutions)

String.raw`templateString`

Parameters

callSite
Well-formed template call site object, like { raw: ['foo', 'bar', 'baz'] }.
...substitutions
Contains substitution values.
templateString
A template string, optionally with substitutions (${...}).

Return value

The raw string form of a given template string.

Exceptions

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 to simulate `t${0}e${1}s${2}t` you can do:
String.raw({ raw: 'test' }, 0, 1, 2); // 't0e1s2t'
// Note that 'test', a string, is an array-like object
// The following is equivalent to
// `foo${2 + 3}bar${'Java' + 'Script'}baz`
String.raw({
  raw: ['foo', 'bar', 'baz'] 
}, 2 + 3, 'Java' + 'Script'); // 'foo5barJavaScriptbaz'

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.raw' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.raw' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 41Edge Full support YesFirefox Full support 34IE No support NoOpera No support NoSafari Full support 10WebView Android No support NoChrome Android Full support 41Edge Mobile Full support YesFirefox Android Full support 34Opera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 4.0nodejs Full support 4.0.0

Legend

Full support  
Full support
No support  
No support

See also

Tag Dokumen dan Kontributor

Terakhir diperbarui oleh: chrisdavidmills,