HomeLanguagesJavascriptJavaScript String raw() Method

JavaScript String raw() Method

JavaScript String.raw() is a static method that is used to get the raw string form of template literals. These strings do not process escape characters. The method is similar to ‘r’ prefix in Python

Syntax:

String.raw(str, ...sub) 
String.raw`templateString`

Parameters: This method takes two parameters

  • str: It is the template literal array object
  • sub: This value contains the substituted values
  • templateString: It is the template literal string

Return Value: Raw string form of the template literal.

Example 1: This example prints a string on the console with and without the raw method.

Javascript




console.log(`DSA\nPython\nJava`);
console.log(String.raw`DSA\nPython\nJava`);


Output: When the raw method is used the escape characters are not processed

DSA
Python
Java
DSA\nPython\nJava

Example 2: This example will use the raw method on Unicode template literals.

Javascript




console.log(`\u2607`);
console.log(String.raw`\u2607`);


Output: The raw method ignores the Unicode declaration and processes the string in its own form

☇
\u2607

Supported Browsers: 

  • Chrome
  • Edge 
  • Firefox 
  • Opera 
  • Safari 

We have a complete list of Javascript string methods, to check those please go through this Javascript String reference article.

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32714 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7088 POSTS0 COMMENTS
Nicole Veronica
12229 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12312 POSTS0 COMMENTS
Shaida Kate Naidoo
7206 POSTS0 COMMENTS
Ted Musemwa
7471 POSTS0 COMMENTS
Thapelo Manthata
7190 POSTS0 COMMENTS
Umr Jansen
7171 POSTS0 COMMENTS