JS Coding Questions Logo
JS Coding Questions
#314💼 Interview💻 Code

What are raw strings

Advertisement

728x90

ES6 provides a raw strings feature using the String.raw() method which is used to get the raw string form of template strings. This feature allows you to access the raw strings as they were entered, without processing escape sequences. For example, the usage would be as below,

javascript
1var calculationString = String.raw`The sum of numbers is \n${
2  1 + 2 + 3 + 4
3  }!`;
4
5console.log(calculationString); // The sum of numbers is \n10!

If you don't use raw strings, the newline character sequence will be processed by displaying the output in multiple lines

javascript
1var calculationString = `The sum of numbers is \n${1 + 2 + 3 + 4}!`;
2
3console.log(calculationString);
4// The sum of numbers is
5// 10!

Also, the raw property is available on the first argument to the tag function

javascript
1function tag(strings) {
2  console.log(strings.raw[0]);
3  }

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 57

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
314of476