Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use backquotes in es6

Shulou Source: shulou.com Published: 2022-06-01 06:58:48 09月28日 Update

This article mainly shows you "how to use backquotes in es6", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn how to use backquotes in es6 "this article.

The use of es6 backquotes: 1. Define a single-line string; 2. Define a multiline string in which spaces, indents and newlines are retained in the output; 3. Embed variables in the string, and the variable name needs to be written in "${}", for example, "`Hello ${name} `"

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

The template string (template string) is an enhanced version of the string, identified by backquotes (`). It can be used as a normal string, to define a multiline string, or to embed variables in a string.

/ / embed variables var name = "Bob", time = "today" in the string; `Hello ${name}, how are you ${time}? `

If you need to use backquotes in the template string, you should escape with a backslash before.

Var greeting = `\ `Yo\ `World!`

If you use a template string to represent a multiline string, all spaces and indents are retained in the output.

$('# list') .html (`first second`)

If you don't want this line break, you can use the trim method to eliminate it.

$('# list') .html (`first second`.trim ())

To embed a variable in the template string, you need to write the variable name in ${}.

The above is all the content of the article "how to use backquotes in es6". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Characters strings quotes variables templates middle content articles multiple lines spaces learning help output normal tutorials slashes methods easy to understand more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Microsoft MySQL Redmi