0

If I am reading correctly the first array is just an array literal, and the second array is accessing an element in the first array.

In the example console.log([5,6,7][1,5,6,2]) the last element of second array is 2, so it is returning the element at index 2 of the first array which is 7.

Why is javascript simply ignoring all other elements in second array and accepting to use the last element in the second array as an index reference? What is happening with contents of second array? why is it not an error that asks for just a number? Also Why not take the first element in the second array why the last?

Another example: console.log(['French Roast', 'Colombian', 'Kona']['1', 'Kona', true, 0]); simply returns 'French Roast'.

to add to question: knowing that the comma 'does' something in javascript, definitely clarifies things a little bit. Am I to understand that commas get 'evaluated' or actually have functionality associated with it in Javascript. I assumed it was just a character to separate elements. is there a function that goes on behind the scenes comparing and sorting when the comma is in play? also how is it stopped when you just want to use as a separation between elements?

  • 3
    There isn't a "second array". You're indexing into the only (first) array and `1,5,6,2` evaluates to `2`. – jonrsharpe Sep 21 '20 at 19:21
  • It's a fair question to ask, but I just want to point out that in practice you wouldn't want to write code like this because to most developers, it isn't obvious what it will output. – Adam Zerner Sep 21 '20 at 19:26
  • This question should not have been closed! While the link hints to the right direction it's not immediately clear what the second pair of brackets actually does (accessing the first array via index). – m02ph3u5 Sep 21 '20 at 19:27
  • @jonrsharpe very interesting, that definitely clarifies things a bit. I did not know that the commas get 'evaluated' or actually has functionality associated with it in Javascript. I assumed it was just a character to separate elements. is there a function that goes on behind the scenes comparing and sorting when the comma is in play? – Vanessa de La Cuetara Sep 21 '20 at 19:29
  • 1
    Maybe it's not the super-duper best dupe target, but it's definitely a dupe. Just use e.g. [this question](https://stackoverflow.com/questions/7421013/why-does-5-6-8-71-2-8-in-javascript) as target, if you feel it's more accurate. – ASDFGerte Sep 21 '20 at 19:32
  • @VanessadeLaCuetara https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator. There's nothing magical here; when you have an array and then access it via `[]` whatever inside is just an expression, here it happens to be one using the comma operator. – Dave Newton Sep 21 '20 at 19:32
  • I think question should be reopened as well, still don't know how the comma has functionality that isn't really defined in the documentation in mozilla docs – Vanessa de La Cuetara Sep 21 '20 at 19:33
  • 1
    @VanessadeLaCuetara it's called the *comma operator*, and it definitely is documented. – Pointy Sep 21 '20 at 19:35
  • @m02ph3u5 Why isn't it clear what the second pair of brackets does? Square brackets explicitly de-reference an object by key, and they're directly after an array; I'm curious what else it could be interpreted as. – Dave Newton Sep 21 '20 at 19:43
  • @m02ph3u5 this article actually answers the comma mystery for me. hope its helpful https://medium.com/javascript-in-plain-english/comma-operator-in-javascript-cfe170f5b4d3 – Vanessa de La Cuetara Sep 21 '20 at 20:10

0 Answers0