What does "array.length -1" mean in JavaScript?

30,415

Solution 1

It's to prevent fencepost errors, aka "off-by-one".

Common exam question:

You are required build 100 meters of fence, with a fence post every meter. How many fenceposts will you need?

Obvious quick (and WRONG) answer:

    100 meters
  --------------- = 100 posts
     1 post
     ------
     meter

Because for 100 meters of fence, you need 101 posts:

Distance:    1 2  ....  99 100
            |-|-|.....| - | - |
Post:       1 2 3 ....99  100 101

Now with arrays, the same thing happens. Let's say it's an array with 5 items:

for (i = 0 ; i <= 5; i++)
                  ^--length of array

You end up doing

        i:    0, 1, 2, 3, 4, 5
iteration:    1  2  3  4  5  6

Oops. 5 item array, but you've executed your loop 6 times - one times too many.

You can fix the error in two ways:

for (i = 0; i < length; i++)
              ^---change from "<=" to "<"

or

for (i = 0; i <= length - 1; i++)
                        ^---change the upper limit value.

Solution 2

If your array has 4 elements, for example

var elementOfArray = [7, 9, 0, 2]

and you want to reach those elements. In that case, you need to know that elementOfArray[i] represents element of your array and i is the index.

For that reason, if you add '-1' on your array length; you can see whole elements in your array like elementOfArray[0]---7, elementOfArray[1]---9, elementOfArray[2]---0, elementOfArray[3]---2. See! you took the whole number in the element.

If you do not subtract 1 of array length, you would take an error. Because there is no elementOfArray[4] that represents any element of your array.

Solution 3

Javascript arrays are 0-based, meaning if you have an array of 5 items, you would use the indices 0 through 4 to access them. They're subtracting one (there should be a space between '-' and '1' to make it more clear) to find the last index.

Edited: If they're subtracting one, the test should be '<='.

Share:
30,415
Adrian Ghinea
Author by

Adrian Ghinea

Updated on August 04, 2022

Comments

  • Adrian Ghinea
    Adrian Ghinea almost 2 years

    I know how to use the JavaScript for loops to cycle through arrays for example, but I still didn't understand what the array.length -1 means, specifically the -1 part.

    When using a for loop over an array we have something like this:

    for (i = 0; i < array.length; i++) {...}
    

    But I've seen also something like this, sometimes:

    for (i = 0; i < array.length - 1; i++) {...}
    

    In the second case, why there is the "-1" in the array.length and what does it do? Also why sometimes there is and sometimes isn't it shown?

  • Adrian Ghinea
    Adrian Ghinea over 8 years
    First of all, thanks Marc for your fast and completed answer. Just a thing: in your for loop you've put in the second condition i <= 5. That gives you the i of 0,1,2,3,4,5 and the iteration of 1,2,3,4,5,6. Why didn't you write directly i < 5?
  • Marc B
    Marc B over 8 years
    remember that for keeps going until that check condition goes false, so... i<=5 will be TRUE for i=5 (array element #6). if you switch to i<5, then when i becomes 5, i<5 becomes false and the loop aborts. for integers, i<5 and i<=4 (aka i<=5-1) are effectively identical
  • IrkenInvader
    IrkenInvader over 8 years
    i < length and i <= length - 1 are equivalent
  • Marc B
    Marc B over 8 years
    choosing which to use is entirely up to the programmer. Personally, I'll with the <= count minus 1 variation, since it makes it clear that you're stopping at one below the count. < count is easy to misinterpret. but that's purely a stylistic choice, and therefore opinion, and therefore off-topic.