javaScript Properties and Methods: String.raw()

The String.raw() method is a tag function of template literals, it was introduced in ECMAScript 6 (ES6) and it is similar to the r prefix in Python or the @ prefix in C# for string literals.

String.raw() is used to get the raw string form of template strings (that is the original, uninterpreted text). It’s syntax goes thus:

String.raw(callSite, ...substitutions)

or String.raw`template string`


callSite is a well formed template call site object (meaning the object is read-only and can only be named raw), an example of such an object is :

{raw: ['apple', 'carrot', 'orange']}

…substitutions contains substitution values.

template string is an incidence where the String.raw() method is called with or without substitutions.

If an object which is not well formed is given as callSite, a TypeError( an error which occurs when a value is not of the expected type is thrown)

Instances of String.raw()

We shall now proceed to find out how String.raw() works. In this example:

var testOne = String.raw `This\n${1+2+3}!`;


One would expect the \n (newline character) to have effect and that the console would log:



With String.raw(), this won’t be the case. What we’re going to get instead is:


Another example:

var testTwo = String.raw ``That\u000A!`;


Just like the previous example, the console will log:


It’s quite evident that with the method String.raw(), all kinds of escape characters are ineffective and backslashes will be present in the output string. This can be confirmed by asking the console to log the .length property of variables testOne and testTwo.

console.log(testOne.length) will log 7

console.log(testTwo.length) will log 11

Actual Uses of String.raw()

String.raw() can be used to show strings without the interpretation of back-slashed characters, it’s also useful in printing Windows sub-directory locations without having to use backslashes a lot.

String.raw() also comes in handy for developers who want to use Steven Levithan’s XRegExp library that accepts texts with significant backslashes. Rather than having to bother about doubling their backslashes, developers can now afford the luxury of writing semantically clear code.

In May, 2016, String.raw() was called out for being inconsistent.

Browser Compatibility

Desktop Browsers such as Google Chrome(41), Microsoft Edge and Mozilla Firefox(34) and Safari 10 support the String.raw() method. Internet Explorer and Opera do not support it.

The mobile browsers known to support String.raw() are Chrome for Android(41), Edge and Firefox Mobile(34).

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.