Skip to content

Instantly share code, notes, and snippets.

@brauliodiez
Created June 4, 2017 09:09
Show Gist options
  • Save brauliodiez/c38f0146444767da76a6d5c54e368564 to your computer and use it in GitHub Desktop.
Save brauliodiez/c38f0146444767da76a6d5c54e368564 to your computer and use it in GitHub Desktop.
SImple ES6 backticks usage

Backticks

Just using the new ES6 backticks to:

  • Display multiline text.
  • Embed variable values into strings (similar to print.format or c# string.format).

Steps

  • Let's first create a multiline text using an ES6 approach:
const myString = 'This is a try to get a long string in several lines' + 
                 'using classic ES5 approach';

console.log(myString);

This approach sucks: is prone to errors.

  • Now let's do the same using backticks:
const myString = `This is a try to get a long string in several lines
                  using classic ES6 backticks`;

console.log(myString);
  • What if we want to display the value of a variable? we can use ${}
const percentage = 80;
const myString = `Total sales increase: ${percentage} %`;

console.log(myString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment