Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
javaScript: Can a comma occur after the last set of values in an array?

I am wondering if a comma trailing an array in javascript is valid?

var settings = {
    'foo'  : oof,
    'bar' : rab,
};

vs

var settings = {
    'foo'  : oof,
    'bar' : rab
};

Note the second example does not have a comma after the last key/value.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Most browsers and implementations do allow a trailing comma, the big BUT is the "Internet Explorer".

A trailing comma in most InternetExplorer versions causes BIG trouble. It'll throw wierd, crazy, strange and unreasonable errors .. you have no idea where you're at! This is terrible, you'll fall into deep and serious depressions. The disease also has a name, "evil comma of doom" it was called once.

Conclusion: NEVER.. ever! use a trailing comma in Javascript.

over 4 years ago · Santiago Trujillo Relatório

0

Historically speaking, ES3 did NOT allow a trailing comma when defining an object literal. This was one thing that IE did get right, but most other bowser vendors went against the spec and allowed it anyways. So technically it was a bug in the other browsers that supported it. In ES3, an ObjectLiteral was defined as,

ObjectLiteral
    { }
    { PropertyNameAndValueList }

Later ES5 resolved this issue by going with the majority and legitimizing the trailing comma by putting it in the spec. Now an ObjectLiteral is defined as,

ObjectLiteral
    { }
    { PropertyNameAndValueList } 
    { PropertyNameAndValueList , }

Notice the trailing comma at the end.

Although the trailing comma is defined in an object literal, it is still not allowed in JSON according to ES5. So while the following object literal is valid,

{ foo: "bar", }

the following JSON is not,

'{ "foo": "bar", }'

The grammar for a JSONObject is,

JSONObject
    { }
    { JSONMemberList }

JSONMemberList
    JSONMember  
    JSONMemberList , JSONMember

JSONMember
    JSONString : JSONValue 

In short, if you don't want to worry about spec or browser quirks, then do NOT add a trailing comma.

over 4 years ago · Santiago Trujillo Relatório

0

With regards to object literals at least (the question's title mentions array, but the example is an object literal) it depends on where your js is running. Trailing commas in object literals are valid in ES5. Although trailing commas can cause problems on the web (in IE) most minifiers will remove them anyway.

http://www.2ality.com/2011/06/object-literal-comma.html

http://dontkry.com/posts/code/trailing-commas.html

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda