3

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?

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 Commented Sep 29, 2017 at 0:07
  • index : 0, 1, 2 still has length of 3 Commented Sep 29, 2017 at 0:09

3 Answers 3

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]
Sign up to request clarification or add additional context in comments.

3 Comments

ok I see, yes that makes perfect sense, I think my problem was i was conceptually trying to get an answer without first breaking it into two separate steps (like your example), Now i see it clearly. THANKYOU
It's also tricky sometimes to see object properties similar to this. You might see object.name, or you might see object['name'] and where it gets tricky is sometimes you see variables in there, like object[something1 + something2] which is "concatenating" (ie: smashing two things together), so it's actually object.something1something2. Those typically show up as object['something' + i] (where i is an iterator number that goes up).
Note you can also use myArray.indexOf('Third') to find the position of the thing you want.
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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.