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
It may be greater than the number of elements if the array is sparse. const clothing = ["shoes", "shirts", "socks", "sweaters"]; console.log(clothing.length); // Expected output: 4 · A nonnegative integer less than 232. The value of the length property is a nonnegative integer with a value less than 232. ... const listA = [1, 2, 3]; const listB = new Array(6); console.log(listA.length); // 3 console.log(listB.length); // 6 listB.length = 2 ** 32; // 4294967296 // RangeError: Invalid array length const listC = new Array(-100); // Negative numbers are not allowed // RangeError: Invalid array length
🌐
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 ...
🌐
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].
🌐
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.
🌐
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.
🌐
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), ...
🌐
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.
Find elsewhere
🌐
Codecademy
codecademy.com › forum_questions › 512d58b828d821a821001009
I really don't understand the minus 1 after the length property, help please? | Codecademy
The first value in the array is [0] (the array number is always one less than a ‘conventional’ count 1-1=0) Second value is [1] (2-1=1) and so on. So the last element is the length of the array - 1
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › javascript array length
JavaScript Array Length Property
November 4, 2024 - The following adds an element to the numbers array at the index 10: let numbers = [10, , 20, 30]; console.log(numbers.length); // 4 numbers[10] = 100; console.log(numbers.length); // 11Copy · In this example, the length property returns 11. JavaScript allows you to change the value of the array length property.
🌐
W3Resource
w3resource.com › javascript › object-property-method › array-length.php
JavaScript length Property : Array Object - w3resource
The length property returns number elements of an array. The value of the length property is an integer and less than 232. The length property is also used to truncate an array. If the length property is set with a number which is greater than ...
🌐
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.
🌐
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>
🌐
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].
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.

🌐
Codecademy
codecademy.com › forum_questions › 506599a816aa7a0002098207
i<array.length or i<=array.length-1 ? | Codecademy
var i; var animals = ["cat", "dog", "ferret"]; for (i=0; i<=animals.length-1; i++) { console.log(animals[i]); }