Template Strings

In traditional JavaScript, text that is enclosed within matching " or ' marks is considered a string. Text within double or single quotes can only be on one line. There was no way to insert data into these strings. This resulted in a lot of ugly concatenation code that looked like:

  1. var name = 'Sam';
  2. var age = 42;
  3. console.log('hello my name is ' + name + ' I am ' + age + ' years old');

ES6 introduces a new type of string literal that is marked with back ticks (`). These string literals can include newlines, and there is a string interpolation for inserting variables into strings:

  1. var name = 'Sam';
  2. var age = 42;
  3. console.log(`hello my name is ${name}, and I am ${age} years old`);

There are all sorts of places where this kind of string can come in handy, and front-end web development is one of them.

原文: https://angular-2-training-book.rangle.io/handout/features/template_strings.html