Skip to content

Instantly share code, notes, and snippets.

@robjens
Created December 10, 2015 21:10
Show Gist options
  • Select an option

  • Save robjens/deac999e469ffd07299b to your computer and use it in GitHub Desktop.

Select an option

Save robjens/deac999e469ffd07299b to your computer and use it in GitHub Desktop.
ES6 tagged template strings, this helped me grok it a bit better
var a = 5;
var b = 10;
var c = 2;
function tag(strings, ...values) {
console.log(strings[0].replace('\n', '')); // "Hello "
console.log(strings[1].replace('\n', '')); // " world "
console.log(values[0]); // 15
console.log(values[1]); // 50
return "Bazinga!";
}
tag`Hello ${ a + b + c }world ${ a * b }`;
// ~ tag("Hello", a + b + c)
// ~ tag("world", a * b)
tag`Hello ${ a + b + c }
world ${ a * b }`;
// now here the \n is part of the second call string, not the first
tag`
Hello ${ a + b + c }
world ${ a * b }`;
// here both are prefixed with a \n newline
// now we can begin to see, these are two parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment