Tuesday, December 20, 2016

JavaScript Evolution ECMAScript 6 - Template String

Template String is string literals allowing embedded expressions.

Below is the typical way we assign a text into a variable.
function buildUser(first, last) {
  let fullname = first + " " + last;
  ...
}
In ES2015, we can wrap the entitled text with backtick `. Any JavaScript need to be evaluated inside the backtick, wrap it with ${...}.
function buildUser(first, last) {
  let fullname = '${first} ${last}';
  ...
}
Template String offers a new and much better way to write multiple strings.
let longText = ' Hi ${username}
This is a very
veeery
long text.

Regards,
  ${admin.fullname}
`;

// Newline characters is preserve and output to screen.
console.log(longText);

No comments:

Post a Comment