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:
$("#result").append( "There are <b>" + basket.count + "</b> " + "items in your basket, " + "<em>" + basket.onSale + "</em> are on sale!" );
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:
$("#result").append( "There are <b>" + basket.count + "</b> " + "items in your basket, " + "<em>" + basket.onSale + "</em> are on sale!" );
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 ? !