Saturday, November 16, 2024
Google search engine
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

Most Popular

Recent Comments