admin管理员组

文章数量:1326461

This question is more of a why does this particular code work this way than a how can I make this code work this way.

Im going through the codecademy tutorials for JavaScript and I came across a lesson that I conceptually can make use of in my own code because I can see the pattern of this particular code - but it doesn't make sense why this code works this way.

Below is an example:

let myArray = ['First','Second','Third'];
var last = myArray[myArray.length - 1];
console.log(last);

The console displays "Third" when running the above code. I know the JavaScript language is a "zero indexed" language and I know that "first" in this array would be position "0" counting from left to right but my question is even if we start at "0" and count every item; 0 then 1 then 2; shouldn't the console log "Second" instead of "Third"? Or does the method of "length" in JavaScript ignore the "0" based indexing system and actually start counting at "1"; in witch case the answer should STILL be "Second" and not "Third"... No matter how my brain adds this up I keep getting "Second" instead of "Third" in my mind even though the output is "third" on the console...

Can anyone explain what I'm missing?

This question is more of a why does this particular code work this way than a how can I make this code work this way.

Im going through the codecademy tutorials for JavaScript and I came across a lesson that I conceptually can make use of in my own code because I can see the pattern of this particular code - but it doesn't make sense why this code works this way.

Below is an example:

let myArray = ['First','Second','Third'];
var last = myArray[myArray.length - 1];
console.log(last);

The console displays "Third" when running the above code. I know the JavaScript language is a "zero indexed" language and I know that "first" in this array would be position "0" counting from left to right but my question is even if we start at "0" and count every item; 0 then 1 then 2; shouldn't the console log "Second" instead of "Third"? Or does the method of "length" in JavaScript ignore the "0" based indexing system and actually start counting at "1"; in witch case the answer should STILL be "Second" and not "Third"... No matter how my brain adds this up I keep getting "Second" instead of "Third" in my mind even though the output is "third" on the console...

Can anyone explain what I'm missing?

Share Improve this question edited Sep 29, 2017 at 0:11 P.S. 16.4k14 gold badges65 silver badges86 bronze badges asked Sep 29, 2017 at 0:05 GhoyosGhoyos 6223 gold badges7 silver badges19 bronze badges 2
  • knowing that myArray.length - 1 == 2 ... now what do you think ... the length is the length, which is 3 ... not 2, because there are 3 elements ... the index is zero based, the length is ... well, the length – Jaromanda X Commented Sep 29, 2017 at 0:07
  • index : 0, 1, 2 still has length of 3 – James Maa Commented Sep 29, 2017 at 0:09
Add a ment  | 

3 Answers 3

Reset to default 4

Try this code out and it will help. This stuff was tricky for me too. It's just using a variable and calculating it at the same time.

let myArray = ['First','Second','Third'];
var last = myArray[myArray.length - 1];
console.log(last);

First, we have to note that myArray.length is 3 because there are 3 items in it.

Second, now we can re-analyze this but exchange myArray.length with 3:

myArray[3 - 1]

Now, you can see it is simply calculating 3-1, which is 2, so the code is identical to:

myArray[2] // which returns the item at position 2 (starting from 0), [0, 1, 2]

Array.length returns the number of items in an array. It has no bearing on the index.

Length - 1 will always be the final index in array though, simply because arrays are 0 indexed in practice.

array.length is a number of items in it, there are 3 items. But array indexes starts from 0 and the last index will be 2, not 3. So if you want to get array element using array.length, you need to decrease this length by 1 if you want to get the last element, by 2 if you want to get the previous one and so on.

本文标签: How to log an index of an array in JavaScriptStack Overflow