Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 506
superUntitled Avatar asked Feb 28 '11 07:02

superUntitled


People also ask

Is trailing commas allowed in JavaScript?

JavaScript has allowed trailing commas in array literals since the beginning, and later added them to object literals, and more recently, to function parameters and to named imports and named exports. JSON, however, disallows trailing commas.

Can JSON have trailing commas?

The JSON file format was introduced in the early 2000s. Since JSON is based on JavaScript's object syntax, and it was invented before ECMAScript 5 was introduced in 2009, trailing commas cannot be used in JSON (remember, trailing commas in object literals became legal in ES5).

What is the last index of an array JavaScript?

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex .

Do you need commas in JavaScript?

JavaScript uses a comma ( , ) to represent the comma operator. A comma operator takes two expressions, evaluates them from left to right, and returns the value of the right expression. In this example, the 10, 10+20 returns the value of the right expression, which is 10+20. Therefore, the result value is 30.


1 Answers

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.

like image 169
jAndy Avatar answered Oct 17 '22 15:10

jAndy