The `raw` method of String class in JavaScript returns a string created from a raw template string.
The syntax of String.raw() static-method is:
String.raw(strings)
String.raw(strings, sub1)
String.raw(strings, sub1, sub2)
String.raw(strings, sub1, sub2, /* …, */ subN)
String.raw`templateString`
This raw() static-method of String returns a string created from a raw template string.
Parameter | Optional/Required | Description |
---|---|---|
strings | required | An array of template strings to process. |
sub1, sub2, ... , subN | optional | Substitutions to replace placeholders in the template strings. |
String.raw() returns value of type String
.
In this example,
name
and age
.String.raw
with a template string containing placeholders for name
and age
.info
.info
to the console.const name = 'John';
const age = 30;
const info = String.raw`Name: ${name}, Age: ${age}`;
console.log(info);
Name: John, Age: 30
In this example,
chars
with characters 'a', 'b', and 'c'.String.raw
with a template string containing placeholders for the array elements.rawString
.rawString
to the console.const chars = ['a', 'b', 'c'];
const rawString = String.raw`Chars: ${chars[0]}, ${chars[1]}, ${chars[2]}`;
console.log(rawString);
Chars: a, b, c
In this example,
stringsList
with strings 'Hello' and 'World'.String.raw
with a template string containing placeholders for the array elements.rawTemplate
.rawTemplate
to the console.const stringsList = ['Hello', 'World'];
const rawTemplate = String.raw`Strings: ${stringsList[0]}, ${stringsList[1]}`;
console.log(rawTemplate);
Strings: Hello, World
In this JavaScript tutorial, we learned about raw() static-method of String: the syntax and few working examples with output and detailed explanation for each example.