There is a function:

function setPageThread (name, options = {}) { let popular = options.popular; let expires = options.expires; let activeClass = options.activeClass; console.log(name, + " " +, popular, + " " +, expires, + " " +, activeClass); // .... } 

I give her the parameters:

 setPageThread("New version out soon!", { popular: true, expires: 1000, activeClass: "is-page-thread" }); 

and get the syntax error:

SyntaxError: expected expression, got ','

If I remove spaces, everything works:

 console.log(name, popular, expires, activeClass); 

I just can not figure out how to add a separator, space or character : Still doing the right thing: I write + ' ' + , but for some reason it does not work.

  • 3
    console.log(name + ", " + popular + ", " + expires + ", " + activeClass); - Alexey Shimansky
  • Once there is a let , maybe destructuring - let {popular, expires, activeClass} = options; . Or even function (name, {popular, expires, activeClass}) { … } . - Aleksei Zabrodskii
  • @elmigranto, and how does this relate to the question? - Grundy

4 answers 4

You incorrectly work with the console, trying to use concatenation where it is not needed. The console.log method supports so-called placeholders that allow you to make the code more visual:

 console.log('%s, %s, %s, %s', name, popular, expires, activeClass); 

For such a simple string, this may be a redundant solution, but it will allow you to develop the right habit.

And you can also use the special syntax of ES6 "templates" in order not to deal with string concatenation manually:

 console.log(`${name}, ${popular}, ${expires}, ${activeClass}`); 

Such "templates" work not only in the console, but also in any other places where a string is expected.

  • Only such templates are not in all browsers yet :) - Grundy
  • @Grundy, yes, because I indicated that this is an ES6 feature. If you wish, you can use a transpiler like Babel. And the TS uses the let question, so the code is most likely already focused on new browsers;) - Dmitriy Simushev

And the commas do not stand there. they should be in brackets

  • you would check then wrote, in brackets they also do not work, I tried - spectre_it

console.log can take multiple parameters.

In this case, the parameters of the function are separated by commas, and when parsing the transmitted expressions an error occurs in the place where after the + stands , what the error text actually says.

To fix, you must either remove the + signs

 console.log(name, " ", popular, " ", expires, " ", activeClass); 

either remove commas

 console.log(name + " " + popular + " " + expires + " " + activeClass); 

    Wrong when forming the argument for console.log . It is understandable, it looks confusing, and typing is inconvenient. In order for the programmer to live well, all the normal browsers put the spaces themselves - just tell what needs to be output to the console:

     console.log(name, popular, expires, activeClass); 

    If you can use ES6, I prefer this option:

     console.log({name, popular, expires, activeClass});