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.
Answer from Marc B on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript - MDN Web Docs
The length data property of an Array instance represents the number of slots in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. It may be greater than the number of elements if the array is sparse.
Discussions

How does arr[arr.length-1] work?
let arr=[1,2]; console.log(arr.length) // 2, 2 elements console.log(arr[0]) // data 1, position 0 console.log(arr[1]) // data 2, position 1 console.log(arr[2])) // error, beyond array scope console.log(arr[arr.length])); // error, beyond array scope console.log(arr[arr.length-1]); // 2 you can also just let last=array.pop(); More on reddit.com
🌐 r/learnjavascript
31
40
September 12, 2021
Shortcut for array[array.length - 1]
While implementing the parser, I've often wished that CoffeeScript supported negative array indexes, to peek at elements towards the end. last: array[-1] Do you all think it would be a worthwhi... More on github.com
🌐 github.com
45
February 16, 2010
What does "array.length + 1" mean in JavaScript - Stack Overflow
I know how to use Javascript for loops to cycle through arrays. But I still don't fully understand what the array.length + 1 is doing, specifically the +1 part. When using a for loop I know it goes... More on stackoverflow.com
🌐 stackoverflow.com
Why is getting the length of an array O(1) and not O(n)?
Most implementations store the length of an array in the object itself. Whenever you insert/remove it gets updated. Thus you're not iterating to get the length you're just getting a value. More on reddit.com
🌐 r/learnprogramming
45
11
November 7, 2022
🌐
Reddit
reddit.com › r/learnjavascript › what does arr.length - 1 mean?
r/learnjavascript on Reddit: What does arr.length - 1 mean?
October 30, 2022 -

I'm going through JavaScript Algorithms and Data Structures Masterclass. One of the problems the instructor gave has got me a little confused. I was hoping someone could enlighten me.

function sumZero(arr) {
    let left = 0;

let right = arr.length - 1;

    while (left < right){
        let sum = arr[left] + arr[right];
        if (sum === 0){
            return [arr[left], arr[right]];
        } else if (sum > 0){
            right--;
        } else {
            left++;
        }
    }
    console.log(left, right)
}

sumZero ([-4,-3,-2,-1,0,1,2,3,10])

I think the arr.length - 1 is supposed to start a loop starting on the number furthest right of loop but if that is the case I'm not sure how the arr.length - 1 makes that happen? Anybody got any clarity regarding this?

🌐
Mimo
mimo.org › glossary › javascript › array-length
JavaScript Array Length: Master Data Handling
Zero-Based Indexing: The length ... length - 1). It Measures Size, Not Data: An array can have a length but contain empty slots, so length indicates the number of slots, not necessarily the number of defined values. ... Become a full-stack developer. Learn HTML, CSS, JavaScript, and React ...
🌐
W3Schools
w3schools.com › jsref › jsref_length_array.asp
JavaScript Array length Property
The length property sets or returns the number of elements in an array.
🌐
CodingNomads
codingnomads.com › javascript-array-length
JavaScript Array Length Property
The length of an array is one more than the highest index due to zero-based indexing in JavaScript. To access the last item in an array, use the expression array[array.length - 1].
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript - MDN Web Docs
July 28, 2026 - The key principle to remember is that only indexes between 0 and arrayLength - 1 are visited, where arrayLength is the length of the array at the time the array method was first called, but the element passed to the callback is the value at the time the index is visited.
🌐
Kevin Chisholm
blog.kevinchisholm.com › javascript › javascript-array-length-always-one-higher
Why is a JavaScript array length property always one higher than the value of the last element's index? | Kevin Chisholm - Blog
February 10, 2021 - So, a JavaScript array with one element will have a “length” of “1”. If a JavaScript array has four elements, then that array’s “length” property will have a value of “four”. But (and here is the point where many get confused), ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › how does arr[arr.length-1] work?
r/learnjavascript on Reddit: How does arr[arr.length-1] work?
September 12, 2021 - arr[arr.length] in the example would mean arr[2], since the length is 2. But there is no index 2 in that array, since arrays start at 0. So the example array has index 0, and index 1. But no index 2, and that's where the error comes from.
🌐
Codecademy
codecademy.com › forum_questions › 512d58b828d821a821001009
I really don't understand the minus 1 after the length property, help please? | Codecademy
To check if you have reached the last value, you can either do <=length-1 or <length, either finishes on the last value stored in the array (n-1)
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › javascript array length
JavaScript Array Length Property
November 4, 2024 - The length property behaves differently depending on the array types including dense and sparse. A dense array is an array where its elements have contiguous indexes starting at zero. For dense arrays, you can use the length property to get the number of elements in the array.
🌐
W3Resource
w3resource.com › javascript › object-property-method › array-length.php
JavaScript length Property : Array Object - w3resource
JavaScript length property returns number elements of an array. The value of the length property is an integer and less than 232.
🌐
Dustin John Pfister
dustinpfister.github.io › 2018 › 12 › 14 › js-array-length
Array length in javaScript and addressing the confusion | Dustin John Pfister at github pages
November 30, 2021 - One way of thinking about array length might be that Array length in javaScript refers to the highest numbered index value of an array plus one because array length is one rather than zero relative.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Arrays
Let’s say we want the last element of the array. Some programming languages allow the use of negative indexes for the same purpose, like fruits[-1]. However, in JavaScript it won’t work. The result will be undefined, because the index in square brackets is treated literally. We can explicitly calculate the last element index and then access it: fruits[fruits.length - 1].
🌐
GitHub
github.com › jashkenas › coffeescript › issues › 156
Shortcut for array[array.length - 1] · Issue #156 · jashkenas/coffeescript
February 16, 2010 - Shortcut for array[array.length - 1]#156 · Copy link · Labels · enhancementwontfix · jashkenas · opened · on Feb 16, 2010 · Issue body actions · While implementing the parser, I've often wished that CoffeeScript supported negative array indexes, to peek at elements towards the end.
Author: jashkenas
🌐
O'Reilly
oreilly.com › library › view › javascript-the-good › 9780596517748 › ch06s02.html
Length - JavaScript: The Good Parts [Book]
May 8, 2008 - var myArray = []; myArray.length // 0 myArray[1000000] = true; myArray.length // 1000001 // myArray contains one property. The [] postfix subscript operator converts its expression to a string using the expression's toString method if it has one. That string will be used as the property name. If the string looks like a positive integer that is greater than or equal to the array's current length and is less than 4,294,967,295, then the length of the array is set to the new subscript plus one.
Author: Douglas Crockford
Published: 2008
Pages: 172
🌐
Edureka
edureka.co › blog › array-length-in-javascript
Array Length In JavaScript | JavaScript Array Length Property | Edureka
February 25, 2025 - The value returned is an unsigned integer. The length property can be specified as: ... <script type="text/javascript"> var music = new Array(); music[0] = "Rock"; music[1] = "Pop"; music[2] = "Jazz"; music[3] = "Blues"; document.write(music.length); </script>
Top answer
1 of 2
2

This

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

loops from indicies 0 to array.length - 1. For example, with an array with 3 items, this iterates over is of 0, 1, and 2.

This

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

is strange. It loops from indicies 0 to array.length. For example, with an array with 3 items, this iterates over is of 0, 1, 2, and 3. This means that if something inside the loop references array[i], that value will be undefined on the final iteration.

Most of the time, seeing something like that would make me think someone made a typo or logic error - though I wouldn't be surprised to see a few algorithms that used something like it.

This

for(let i = 1; i < array.length; i++)

iterates from indicies 1 to array.length - 1. For example, with an array with 3 items, this iterates over is of 1, and 2.

There will be one less iteration than elements in the array. This means that, if the loop body always references array[i], and not array[i - 1], then the first element in the array will always be skipped over.

This is much less common than starting at 0, but could be seen, especially when the logic requires comparing certain adjacent elements against each other (for example, array[0] to array[1], and array[1] to array[2], and so on).

2 of 2
0
Array.length + 1

simply put is just the number of elements in the array and add 1.

An array with n elements has a length of n , so your loop will run n - i times when using array.length. If let i =0 , then your loop will run n times. If i = 1 , then your loop will run n-1 times if using array.length. So you will be missing 1 iteration of your loop.

If you need to use i=1 to start your iterations, you should use array.length +1 to get the equivalent number of iterations.

for(let i =0;i<array.length;i++){} // n iterations

is the same number of iterations as

for(let i= 1;i<array.length +1;i++){}  // n iterations
for(let i=1;i<array.length;i++){}  // n-1 iterations 

will always be 1 less iteration.

And remember, Array indexing uses a 0-based counter, so array[0] is the first element of every array.

🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Arrays
The W3Schools online code editor allows you to edit code and view the result in your browser