ES6 Template String template string

Template String is an enhanced version of the string, marked with backticks (`), it can be used as a normal string, it can also be used to define a multi-line string, or to embed variables in the string.

You can look at the following piece of code:

copy code
$("#result").append(
  "There are <b>" + basket.count + "</b> " +
  "items in your basket, " +
  "<em>" + basket.onSale +
  "</em> are on sale!"
);
copy code
 

We use a bunch of '+' signs to connect text and variables, and using ES6's new feature template string ``, we can write it directly like this:

$("#result").append(`
  There are <b>${basket.count}</b> items
   in your basket, <em>${basket.onSale}</em>
  are on sale!
`);

Isn't it cool to use backticks ` to mark the beginning, ${}to quote variables , and all spaces and indentation are preserved in the output ? !

Template String is an enhanced version of the string, marked with backticks (`), it can be used as a normal string, it can also be used to define a multi-line string, or to embed variables in the string.

You can look at the following piece of code:

copy code
$("#result").append(
  "There are <b>" + basket.count + "</b> " +
  "items in your basket, " +
  "<em>" + basket.onSale +
  "</em> are on sale!"
);
copy code
 

We use a bunch of '+' signs to connect text and variables, and using ES6's new feature template string ``, we can write it directly like this:

$("#result").append(`
  There are <b>${basket.count}</b> items
   in your basket, <em>${basket.onSale}</em>
  are on sale!
`);

Isn't it cool to use backticks ` to mark the beginning, ${}to quote variables , and all spaces and indentation are preserved in the output ? !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325263812&siteId=291194637
Recommended