0

I was working on a Node script, and suddenly I got errors when accessing this array:

var a = [
    ["foo", "bar"],
    ["ham", "eggs"]
    ["yin", "yang"]
];

Obviously, my error was the missing comma after the second element of the array. But why the script compiles without syntax errors? In Node:

> ["ham", "eggs"] ["yin", "yang"];
undefined
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
  • 3
    1. You first defined an array `["ham", "eggs"]`, 2. You try to get value from it. 3. `"yin", "yang"` would be eval to `"yang"`, so it should be equal to `var anArray = ["ham", "eggs"]; anArray['Yang'];` And there's no key `'yang'` , so you get `undefined`. – fuyushimoya Sep 21 '15 at 10:37
  • Really amazed !!! It shows array structure like `[["foo","bar"],undefined]` – Rayon Sep 21 '15 at 10:37
  • `object['key']` looks for a value inside `object` with `key`. So, it is not really a syntax error. It just looks for `["yin", "yang"]` key inside `["ham", "eggs"]` object and returns `undefined`. – Ozan Sep 21 '15 at 10:39
  • Thank you all! Comma operator, sigh – lorenzo-s Sep 21 '15 at 11:31

0 Answers0