2

For example when I text the following code, the answer is 10

var testArry = [20, 10, 90, 50, 66][1];
console.log(testArry); // output: 10

When i add another index the first index is overriding, the answer is 90

var testArry = [20, 10, 90, 50, 66][1,2];
console.log(testArry); // output: 90

What is the reason for overriding? Is this overriding or any other terminology is there for this?

I googled it but not found the desired reason.

Thanks.

Ramlal S
  • 1,573
  • 1
  • 14
  • 34

1 Answers1

1

In your array index expression (the 1,2) you are using the comma operator, which simply returns the last result, in this case a two, and does nothing with the others (the 1). Therefore you're basically just writing testArray[2].

Robert Moore
  • 2,207
  • 4
  • 22
  • 41
  • Additional notes: Basically, it is just using the last value. If you had [1,2,3] 3 is used resulting in 50. If you specified 8 as the last value then the results would be undefined as the index would be out of bound. – edjm May 15 '20 at 12:17