JavaScript String raw() Method



The JavaScript String raw() is a static method used to retrieve the raw form of template literals. This method preserve the literal content without interpeting escape sequences.

If the value of the first argument does not the raw property, or the new property is undefined or null, the method will throw a 'TypeError' exception.

Syntax

Following is the syntax of JavaScript String raw() method −

String.raw(strings, sub1, sub2, /* , */ subN)

Parameters

This method accepts two parameters named 'strings' and 'sub1, sub2, ... subN', which are described below −

  • strings − The template literal array object.
  • sub1, sub2,....subN (optional) − The substituted values.

Return value

This method returns the raw string form of the template literal.

Example 1

If we pass only the string parameter value to this method, it will return a raw string form of this template literal.

In the following example, we are using the JavaScript String raw() method to retrieve a raw string form of this template literal "C:\Apache24\htdocs\javascript\JavaScript String Methods".

<html>
<head>
<title>JavaScript String raw() Method</title>
</head>
<body>
<script>
   const string =  "C:\Development\profile\aboutme.html";
   document.write("The given string: ", string);
   //using the String.raw() method
   const filePath = String.raw`C:\Development\profile\aboutme.html`;
   document.write("<br>The raw form: ", filePath);
</script>
</body>
</html>

Output

The above program returns the raw form of the given template literals.

The given string: C:Developmentprofileaboutme.html
The raw form: C:\Development\profile\aboutme.html

Example 2

The following is another example of the JavaScript String raw() method. We use this method to retrieve the raw form from the given template literal "Hi\n${20+5}!".

<html>
<head>
<title>JavaScript String raw() Method</title>
</head>
<body>
<script>
   const string = `Hi\n${20+5}!`;
   document.write("The given string: ", string);
   //using the String.raw() method
   const filePath = String.raw`Hi\n${20+5}!`;
   document.write("<br>The raw form: ", filePath);
</script>
</body>
</html>

Output

After executing the above program, it will return the raw of the given template literal.

The given string: Hi 25!
The raw form: Hi\n25!

Example 3

If the value of the first argument ( string) doesn't have the raw property or it has a null value, it will throw a 'TypeError' exception.

In the example below, we use the JavaScript String raw() method to retrieve the raw form of this template litral. We pass the null value as a first argument to this method. Since, the passed value is invalid, it will throw an exception.

<html>
<head>
<title>JavaScript String raw() Method</title>
</head>
<body>
<script>
   const string = null;
   document.write("The given string: ", string);
   try {
      //using the String.raw() method
      const filePath = String.raw(string);
      document.write("<br>The raw form: ", filePath);
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

Once the above program is executed, it will throw a 'TypeError' exception.

The given string: null
TypeError: Cannot convert undefined or null to object
Advertisements