0

Should we write

var a = [ 'a', 'b', 'c', ];
var b = { '1', '2', '3', };

or

var a = [ 'a', 'b', 'c' ];
var b = { '1', '2', '3' };

What is the most correct way?

I've noticed that old versions of IE raise error if there is comma expecting there would be a another array item (or property) after comma.

s.webbandit
  • 16,332
  • 16
  • 58
  • 82

4 Answers4

5

I've noticed that old versions of IE raise error if there is comma

That is reason enough to not put a comma after the last element! However, it is valid to do so.

Note however that your 2nd example...

var b = { '1', '2', '3', };

...will throw a syntax error. I'm guessing you intended to make it an object literal, but just made a mistake when writing the question:

var b = { x: '1', y: '2', z: '3', }; //Object literal, no syntax error :)
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 1
    +1. However it is not a valid JSON - another reason to avoid it. – freakish Oct 07 '12 at 18:53
  • 1
    @freakish: What does JSON have to do with it? JSON notation also requires double quotes, but that just doesn't seem relevant when using object literal notation. – I Hate Lazy Oct 07 '12 at 18:59
  • 2
    @user1689607 - Nothing really, but since JSON is just a very small, strict subset of JavaScript, it's worth a mention. However, it's probably worth noting that an array/object literal with trailing comma *will* still stringify to valid JSON. – James Allardice Oct 07 '12 at 19:02
  • @user1689607 Double quotes is a good practice as well. It is unlikely that OP is going to write his own JSON serializer/deserializer, but since JSON is extremely popular nowadays ( and it is directly related to JavaScript ) it is just worth a mention. – freakish Oct 07 '12 at 19:07
1

Both are correct according to the ECMAScript 5 specification.

Although this has nothing to do with JavaScript, FWIW, the trailing comma is not allowed in JSON.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

I just laugh at this situation :)

// Firefox and Chrome 
["a", "b",].length // 2

// ie7
["a", "b",].length // 3 :) 
yasaricli
  • 2,433
  • 21
  • 30
  • This makes me sad. Direct contravention of the ES5 spec: "If an element is elided at the end of an array, that element does not contribute to the length of the Array." I'd expect it in IE < 9, but not really IE9 itself. However, worth noting that `["a", "b",,].length === 3` in correct implementations. – James Allardice Oct 07 '12 at 19:12
  • Oh, I just tried it. IE9 returns `2`, so it's not so bad. In IE8 and below you can just always expect things like that. – James Allardice Oct 07 '12 at 19:14
  • yes sample [11.1.4 Array Initialiser] --> http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf – yasaricli Oct 07 '12 at 19:16
  • @JamesAllardice sorry ie9 change ie7 ok? – yasaricli Oct 07 '12 at 19:18
  • I'm not sure if IE8 does it or not as I don't have it to hand to test. I didn't downvote by the way. – James Allardice Oct 07 '12 at 19:19
  • ie7 commas in a funny way :) length – yasaricli Oct 07 '12 at 19:21
0

The most correct way is without a trailing comma as that implies something is to follow.

Jordan Denison
  • 2,649
  • 14
  • 14