0

I was looking at gruntjs and I looked at some JSON examples used to configure Grunt tasks.

Here is an example of the JSON:

grunt.initConfig({
  concat: {
    foo: {
      // concat task "foo" target options and files go here.
    },
    bar: {
      // concat task "bar" target options and files go here.
    },
  },
  uglify: {
    bar: {
      // uglify task "bar" target options and files go here.
    },
  },
});

As you can see, there is an 'extra' comma after each of the bar properties. I tried this notation in Chrome and it is valid. Although it is valid, I wouldn't use this notation but why would people use it?

ontk
  • 942
  • 1
  • 10
  • 24
  • 7
    So that you can append to the list without worrying about whether or not there was a comma on the previous line. – Waleed Khan Aug 08 '13 at 01:25
  • 4
    @WaleedKhan also if you add later lines you don't need to edit the previous line to add a trailing comma, thus keeping the revision history a little tidier – John Carter Aug 08 '13 at 01:49
  • possible duplicate of [Trailing commas in JavaScript](http://stackoverflow.com/questions/7246618/trailing-commas-in-javascript) –  Apr 12 '14 at 15:49
  • 4
    a) it's not JSON, because in JSON this syntax is invalid. b) It's valid in pure JS based on ES >= 5; c) both the title and the first sentence here is invalid. –  Apr 12 '14 at 15:50
  • Please remove "JSON" from the post and title, and I may remove my downvote. This post has absolutely nothing to do with JSON. – rdb Aug 15 '16 at 13:05
  • A key/value pair (`key: value`) is not called a "value assignment", please get your terminology straight. –  Aug 18 '16 at 17:12

1 Answers1

4

I tried this notation in Chrome and it is valid.

Simply because it works in Chrome doesn't mean it's valid. It is valid because the spec says so :-)

I wouldn't use this notation but why would people use it?

To make copy&pasting easier. You can just append new properties without caring additional work. It's a bad practice in program code because older browsers (notably IE) and the ES3 spec disallow them, but in a config file (i.e. in a known environment) it makes life easier.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    Trailing commas have always been valid. The ES3 spec did not disallow them, according to the ES5 spec at http://ecma-international.org/ecma-262/5.1/: "11.1.4: Edition 5 clarifies the fact that a trailing comma at the end of an ArrayInitialiser does not add to the length of the array. This is not a semantic change from Edition 3 but some implementations may have previously misinterpreted this." – Glen Nov 22 '13 at 05:23
  • 2
    @Glen: This question is about object initialisers, not about arrays :-) – Bergi Nov 22 '13 at 09:57
  • 1
    oops, you're right, but this one looks like the right one, and it seems to say that a trailing comma is optional: http://ecma-international.org/ecma-262/5.1/#sec-11.1.5 – Glen Jan 11 '14 at 05:27
  • You should mention the advantages for diffing. –  Aug 18 '16 at 17:10