Why do 1st index is overriding in javascript array when 2nd index is added?

Viewed 39

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.

1 Answers

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].

Related