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.
grammar
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.
Example 1 (multiline string)
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);
Example 2 (Quotation marks in string)
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 id="Using-the-i-template-string-literals-i-to-add-quotes-in-the-string">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>
Example 3 (variables in string)
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 id="Using-the-i-template-string-literals-i-to-add-variables-in-the-string">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>
Example 4 (expression in string)
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 id="Using-the-i-template-string-literals-i-to-add-expression-in-the-string">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>
Example 5 (HTML in string)
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 id="Using-the-i-template-string-literals-i-to-add-HTML-to-the-document">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!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools