Skip to content

Instantly share code, notes, and snippets.

@mwq27
Last active April 16, 2017 19:38
Show Gist options
  • Save mwq27/253383cb7e961e4c1e1235200609f071 to your computer and use it in GitHub Desktop.
Save mwq27/253383cb7e961e4c1e1235200609f071 to your computer and use it in GitHub Desktop.
function foo(strings, ...values) {
return strings.reduce(function(str, val, idx) {
/*
We do the 'idx > 0 ? values[idx - 1]' part because of the starting point in the accumlator ('').
That will be the first section of the string.
*/
return str + (idx > 0 ? values[idx - 1] : '') + val
}, '');
}
const name = 'Marques';
const city = 'Chicago';
console.log(
foo`My name is ${name} from ${city}. Is your name ${name} as well?`
)
// "My name is Marques from Chicago. Is your name Marques as well?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment