0

Still learning JS, I have a new problem I don't understand. I have an array containing two arrays. I would like to concatenate all the values from the first one. What I don't understand is when I type :

var test1 = [2, "wine", "ok"];
var test2 = ["Ok", "hello"];
var arrayArrays = [test1, test2];

alert(arrayArrays[0, 1]);

I get :

Ok,hello

(in an alert).

What I don't understand is what does 0 and 1 stands for ?

I understand that if I type

alert(arrayArrays[0][1]);

I get hello, but I don't understand what it means when I type [0,1].

It strange cause if I type :

alert(arraysArrays[2, 1]);

I still get 'Ok, hello'.

Can someone help me please ?

  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – Bergi Nov 15 '16 at 01:44
  • 2
    Bergi's link is spot-on, but a slightly more detailed explanation is that there is nothing going on with arrays there. Inside the `[0, 1]` expression, the JS engine is simply reading `0, 1` using the comma operator, which essentially evaluates the operands and returns the last one, namely, `1`. So `arrayArrays[0, 1]` is equivalent to `arrayArrays[1]`. – Scott Sauyet Nov 15 '16 at 01:53

0 Answers0