I'm using tagged template strings in following code
var a = 5;
var b = 10;
var pp="";
function tag(strings, ...values) {
pp+=strings[0]; // "Hello "
pp+=strings[1]; // " world "
pp+=values[0]; // 15
pp+=values[1]; // 50
console.log(pp+"Bazinga!");
}
tag`Hello ${ a + b } world ${ a * b}`;
but it gives
Uncaught SyntaxError: Unexpected token ...(…)
On function tag(strings, ...values) {
To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.
The error Uncaught SyntaxError: Unexpected token < is most commonly caused by your site's code referring to an asset that is no longer available. Most commonly, this is due to a filename change in assets generated during your build.
As the syntax error Unexpected token ...
tells you, not the tag is the problem, but the usage of the rest operator. Try the following:
var a = 5,
b = 10;
function tag(strings) {
var pp="";
pp+=strings[0]; // "Hello "
pp+=strings[1]; // " world "
pp+=arguments[1]; // 15
pp+=arguments[2]; // 50
return pp+"Bazinga!";
}
console.log(tag`Hello ${ a + b } world ${ a * b}`);
According to the ES6 compatibility table, you need to enable rest syntax via the harmony flag in the current Chrome.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With