0

Why the string parameter behaves differently depending on how it was defined?

function a(s) {
    console.log(...s)
}
a (`cde`)  //prints c d e
a ('cde')  //prints c d e
a `cde`    //prints cde
a 'cde'    //returns Uncaught SyntaxError: Unexpected string. WHY???

If both of these return "string", why the different behavior?

typeof(`cde`)  //string
typeof('cde')  //string
  • 2
    `a \`cde\`` is tagged template literal syntax meaning `a('cde')`. `a 'cde' ` is just a syntax error has nothing to do with ho you "define" string – Yury Tarabanko Jan 22 '21 at 12:11
  • 2
    @YuryTarabanko "*meaning `a('cde')`*" - no, it means `a(['cde'])` (detailed semantics are even more complicated, it's the same immutable array in each call). Notice the OP used spread syntax in his `console.log` call. – Bergi Jan 22 '21 at 13:14
  • @Bergi, yeah right. – Yury Tarabanko Jan 22 '21 at 13:57

0 Answers0