6

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) {

5
  • 3
    What JS engine are you testing your ES6 code in? Commented Oct 14, 2015 at 12:38
  • Are you using a transpiler? Do template strings work in your environment without tags? Commented Oct 14, 2015 at 12:46
  • yes it does, I'm using latest chrome console Commented Oct 14, 2015 at 12:48
  • Worksforme Commented Oct 14, 2015 at 12:49
  • 3
    Chrome doesn't support rest parameters yet: kangax.github.io/compat-table/es6/#test-rest_parameters Commented Oct 14, 2015 at 12:53

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Cool, it worked but didn't got how to enable rest syntax
@AkhileshKumar Put this link in the address bar and activate the highlighted option: chrome://flags/#enable-javascript-harmony

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.