In the ES6 version of JavaScript, literals were introduced. JavaScript includes object literals, array literals, numeric literals, RegExp literals, etc. Additionally, it contains string literals.
String literals allow us to create multi-line strings without any backslash characters, add any word or sentence in quotes, and add variables and mathematical expressions between strings.
Users can use template string literals in ECMAScript 6 according to the following syntax.
let string = `This is template literal string!`;
In the above syntax, we use backticks (``) to create template literal strings.
In the following example, we use a template literal string to create a multiline string. Whenever we create a quoted string, we need to use "
" characters to create a new line, but with string literals, we can do that by writing the string in a new line.
In string1, a new line is created by writing the string in a new line, while in string2, we use "
" characters to create a new line. The user can observe string1 and string2 in the output and they appear to be the same.
let string1 = `This is first line. This is the second line. This is the third line. This is the fourth line.`; console.log(string1); // added character to create a multiline string. let string2 = "Welcome on the TutorialsPoint!"; console.log(string2);
We can add quotes inside a string using template string literals. When we create a string with double quotes, we can only add single quotes to that string and when we create a string with single quotes, we can only add double quotes to that string.
We added single quotes to the string in the stringQuote variable using string literals.
<html> <body> <h2>Using the <i>template string literals</i> to add quotes in the string.</h2> <div id = "output"></div> </body> <script> var output = document.getElementById('output'); let stringQuotes = `This is a 'template string literals' with a quote.`; output.innerHTML += stringQuotes + "<br/>"; let string1 = "This is 'similar to template string literals'." + "<br/>"; output.innerHTML += string1; </script> </html>
In the following example, we have completed variable substitution in the string. Generally, to use variables in a string, we need to use the " " operator and concatenate multiple strings, but template string literals allow us to add variables directly in a string. We can add variables in ${} expressions.
In the value of the variableStr variable, we inserted the name, job and timePeriod variables.
<html> <body> <h2>Using the <i>template string literals </i> to add variables in the string.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let name = "Shubham"; let job = "Content writer"; let timePeriod = "1 Year"; let variableStr = `Using template string literals :- ${name} is a ${job} at TutorialsPoint from last ${timePeriod}.`; output.innerHTML += variableStr + "<br/>"; let string = "Using Quotes :- " + name + " is a " + job + " at TutorialsPoint from last " + timePeriod + ". "; output.innerHTML += string + "<br/>"; </script> </html>
In this example, we will use template string literals to add mathematical expressions to strings. In sumString, we added the mathematical expression inside ${}. The user can see how we sum num1 and num2 in a string.
In addition, we also multiplied the 2 values in string2.
<html> <body> <h2>Using the <i> template string literals </i> to add expression in the string.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let num1 = 10; let num2 = 40; let sumString = `The sum of ${num1} and ${num2} is ${num1 + num2}`; output.innerHTML += sumString + "<br>"; let string2 = `The multiplication of 20 and 5 is ${20 * 5}`; output.innerHTML += string2 + "<br>"; </script> </html>
We can use template string literals to create a line of HTML and add it to a web page. In this example, we create a list of HTML using a string literal and add rows of HTML to the web page using the innerHTML property.
<html> <body> <h2>Using the <i>template string literals</i> to add HTML to the document.</h2> <div id = "output"> </div> </body> <script> var output = document.getElementById('output'); let HTMLString = `<ul> <li> One </li> <li> Two </li> <li> Three </li> <li> Four </li> <li> Five </li> </ul>`; output.innerHTML = HTMLString; </script> </html>
Users learned to use template string literals in JavaScript. We've seen how to create multi-line strings, variable and expression substitution, adding quotes, and using template string literals to create lines of HTML.
The above is the detailed content of How to use template string literals in ECMAScript 6?. For more information, please follow other related articles on the PHP Chinese website!