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.
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.
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).
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With