-3

In the Go language, a trailing comma is required when creating a struct object, so in Go a comma is a terminator.

var a mystruct = {
  a: 5,
  b: 6,
}

In the Rust language, it seems to be good style to treat commas like terminators, but the language also allows them to be like separators, it's up to you.

In JavaScript, it used to behave like a separator only. However, newer versions of JavaScript allow trailing commas.

let a = {
  a: 5,
  b: 5,
};
  • Does the JavaScript spec still consider a comma to be a "separator" (inside object and array literals and function parameter list) or do they now consider it either a "separator" or a "terminator"?
  • Is it wrong for me to say that the comma is a "terminator" in the example above?
David Callanan
  • 5,601
  • 7
  • 63
  • 105
  • 2
    You should add more context. In what context do you think trailing commas are required in Go? Could you add code examples? – justinas Mar 23 '20 at 12:10
  • 1
    Can you give some context for the trailing commas? Seems like you're talking about the array and object literal syntax but there is also the [comma operator](https://stackoverflow.com/questions/3561043/what-does-a-comma-do-in-javascript-expressions) which is a completely separate thing. – VLAZ Mar 23 '20 at 12:11
  • "*Is it wrong for me to say that a comma is a "terminator"?*" I'd say yes - very wrong. I'm not really sure if we're talking about the same thing, though. Can you give a sample code where you'd call a comma a terminator? – VLAZ Mar 23 '20 at 12:12
  • Commas are allowed to trail in arrays and objects. They are not terminators, just trailing separators – mplungjan Mar 23 '20 at 12:13
  • If by "terminator" you mean "line terminator", then no, a comma is not a line terminator: https://www.ecma-international.org/ecma-262/10.0/#sec-line-terminators . I also don't think it's a terminator in other languages, otherwise would `[]{1,2,3}` be valid in Go? – Felix Kling Mar 23 '20 at 12:15
  • If these terms would have absolute meaning everybody agrees to one might be able to answer your question. – Volker Mar 23 '20 at 12:44
  • @Volker assuming separator means that it goes in between, and terminator means it goes at the end of each – David Callanan Mar 23 '20 at 17:29

1 Answers1

2

A trailing comma is not a terminator in JS

JS does not "allow trailing commas"

var a,b,c,

function test() {}

It allows trailing separators:

console.log(["a",",b","c",])
mplungjan
  • 169,008
  • 28
  • 173
  • 236