-1

The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

let array = [1, 2, 3];
console.log(array.length);

Output = 3

Luka Momcilovic
  • 159
  • 2
  • 16
  • I know, exactly. So ... 0 = 1, 1 = 2, 2 = 3 Why doesn't it give me 3?? – Luka Momcilovic May 01 '19 at 13:44
  • 1
    your output should be 3? the index starts at 0 but the length is the total number of elements present. – Doug May 01 '19 at 13:44
  • For me its showing `3` – Maheer Ali May 01 '19 at 13:44
  • Let me double check. – Luka Momcilovic May 01 '19 at 13:45
  • 4
    Are you asking why a 3-element array has length 3? – John Coleman May 01 '19 at 13:46
  • Yes, why? It should be 2 – Luka Momcilovic May 01 '19 at 13:47
  • The `length` of the array is the total number of elements in that array. What exactly is the confusion? – adiga May 01 '19 at 13:48
  • Why? By definition the length of an array is the number of elements. 0,1,2 is a list of 3 numbers. – John Coleman May 01 '19 at 13:48
  • So, I think the confusion is that JavaScript starts /counting/ at 1 ... 1 element = 1 ... but JavaScript stores data in the 0 index of an array. – Doug May 01 '19 at 13:48
  • I have three things, but you are saying it should be 2, because the index starts at zero and not 1?? That is a new argument I have not heard. – epascarello May 01 '19 at 13:49
  • 1
    ahhh, I think I get it now. If I wanted to change the the third element I would have to refer to it this way array[2] = "poop". The length and the index are two different things. I see. – Luka Momcilovic May 01 '19 at 13:49
  • Actually let me give it another shot -- the index starts at 0, where the first element lives. But JS does start counting at 0 when there is nothing present. `[].length === 0` – Doug May 01 '19 at 13:50
  • Just a guess -- you are a VBA programmer? In VBA there is no array length function, but there is a `UBound` function which is used in its place. `UBound(A) = 2` if `A = Array(0,1,2)`. If so, it is a natural mistake to expect that `length` would work like `UBound`. – John Coleman May 01 '19 at 13:50
  • https://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection – Marie May 01 '19 at 13:53
  • @JohnColeman haha, nope, I'm a CodeAcademy wannabe javascript programmer – Luka Momcilovic May 01 '19 at 13:53
  • Whether or not you are a VBA programmer, it isn't an unreasonable point of confusion for someone starting out in programming. I don't think that the downvotes are really warranted. – John Coleman May 01 '19 at 13:55
  • I didnt downvote but others may have because this question has been asked a lot. Here is one of the more upvoted answers https://stackoverflow.com/a/300540/2647442 – Marie May 01 '19 at 13:57
  • @JohnColeman, oh the downvotes, they burn, but I need to understand this so ...let the downvotes come hehe – Luka Momcilovic May 01 '19 at 14:04
  • [This post](https://dev.to/adarshgoyal/why-array-index-in-c-starts-from-0-not-1-119h) at dev gives a reason why starting at 0 is preferable. – Matt Ellen Aug 11 '20 at 11:50

2 Answers2

5

The first element is at index 0. The length is the number of elements. Counting and indices are unrelated. If indices started at 99 but there were only four elements, the length would not be 103, it would be 4. Likewise of the first index was -66 and there were only 6 elements the list would not be -60 elements long.

I agree it can be confusing. Maybe think of indices as names*. So each place in an array has a name. For our convenience the names are related to the position in the array of each element.

The positions start counting from 0, but the number of elements starts counting from 1, as Marie says in her comment.


*In fact in javascript they are names, as all indices are also properties.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • The first element is 0. The second element is 1. The third element is 2. I should be getting 2, not 3. – Luka Momcilovic May 01 '19 at 13:47
  • 2
    Length and indexing are entirely separate concepts.The "Length" is the number of items in a collection, starting at one. The last index is `Length - 1` – Marie May 01 '19 at 13:48
  • @LukaMomcilovic - No. The array has 3 elements, hence it is showing 3. This is correct. As Matt pointed out, the indices are a different thing. – Jakub Judas May 01 '19 at 13:49
1

The OP original stated that the following code returned 4 when the actual value is 3.

let array = [1, 2, 3];
console.log(array.length);

The problem is you're getting confused between the .length property which gives you the number of items in the array, and the index of the items.

Last index of the array is 2, because the index starts at 0... however, the length will remain as 3

freefaller
  • 19,368
  • 7
  • 57
  • 87
  • I didn't downvote. But yea, It's 3, but I still don't get it. I should be getting 2, because there are 3 elements in the array, and javascript starts counting from 0. – Luka Momcilovic May 01 '19 at 13:47
  • I downvoted because the question is "Why is length higher than the highest index?" and you are answering the inverse. It also doesn't actually explain anything, just demonstrates what the OP already understands to be the functionality. – Marie May 01 '19 at 13:47
  • @LukaMomcilovic `length` doesnot mean the last index. `length` means total elements in array. – Maheer Ali May 01 '19 at 13:48