0

The following case returns undefined in Chrome devtools.

['test1', 'test2']['test3', 'test4'].

I believe this may be because of automatic semicolon insertion, but this case I'm finding hard to understand when running in the Chrome devtools.

The following (and other permutations of ; in different locations) returns the last item, ['test3', 'test4']:

;['test', 'test2'];['test3', 'test4'];

I believe this case is different from the marked duplicate because the usage of the comma doesn't cause any of the individual items to be returned (it makes sense that 'test3','test4' would return 'test4', but in this instance the commas are within the context of arrays).

Community
  • 1
  • 1
Way Spurr-Chen
  • 405
  • 2
  • 9
  • To expand on the duplicate: ASI never inserts a semicolon without a line break. Instead, the comma operator is making the expression equivalent to `['test1', 'test2']['test4']`. The comma operator is a mistake. – Ry- Jan 19 '17 at 17:21
  • Are you sure? The expression returns `['test3', 'test4']` - it doesn't omit `'test3'`. The comma operator doesn't seem like it would be a mistake or illegal within the context of an array, unless the brackets are being interpreted as something else. – Way Spurr-Chen Jan 19 '17 at 18:59
  • The brackets are indeed being interpreted as something else: bracketed property access. Consider `var a = ['test1', 'test2'];`. `a[0]` is `'test1'`. `a[1]` is `'test2'`. `a[2]` is `undefined`. `a['test4']` is `undefined`. `a['test3', 'test4']` is `undefined`. `['test1', 'test2']['test3', 'test4']` is `undefined`. – Ry- Jan 19 '17 at 20:21
  • `;['test', 'test2'];['test3', 'test4'];` is completely different (two statements); see https://stackoverflow.com/questions/8618270/using-a-blocks-return-value-in-javascript – Ry- Jan 19 '17 at 23:43

0 Answers0