Start learning free

LESSON 99 OF 410 · ARRAYS I

Array Length in JavaScript

Lesson Read the full explanation

We can get the length of an array using the .length property like this:

let ourArray = [50, 40, 30];

// Printing the length:
console.log(ourArray.length); // Output: 3

// Changing the array:
ourArray.pop();

// Printing the new length:
console.log(ourArray.length); // Output: 2

Using the length

We can use the length to access items from the end of an array.

Because arrays are 0-indexed, the index of the last item is length - 1:

let ourArray = [50, 40, 30];

// Printing the last item:
console.log(ourArray[ourArray.length - 1]); // Output: 30

// Printing the second to last item:
console.log(ourArray[ourArray.length - 2]); // Output: 40

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · ARRAYS I

    Array Length in JavaScript, explained

    JavaScript array.length: What It Is and How to Use It

    Almost every piece of code that works with arrays needs to know how many elements are in them — to loop through them safely, to grab the last item, to validate input, or to guard against empty arrays. The .length property gives you this instantly. It's not a method call (no parentheses needed), it updates automatically as the array changes, and understanding it prevents one of the most common beginner bugs: off-by-one errors.

    Reading the Length

    const scores = [95, 82, 74, 88, 91];
    
    console.log(scores.length);   // 5
    
    // length updates automatically when the array changes:
    scores.push(77);
    console.log(scores.length);   // 6
    
    scores.pop();
    console.log(scores.length);   // 5
    

    Accessing the Last Element

    Because arrays are zero-indexed, the last element lives at index length - 1. This pattern comes up constantly:

    const letters = ["a", "b", "c", "d", "e"];
    
    const last   = letters[letters.length - 1];    // "e"
    const second = letters[letters.length - 2];    // "d"
    
    // Modern ES2022 shorthand — cleaner for the last element:
    console.log(letters.at(-1));   // "e"
    console.log(letters.at(-2));   // "d"
    

    Using Length in Loops

    The classic for loop uses length as the upper bound. The loop condition i < array.length (strict less-than, not ≤) is what keeps you from accessing an out-of-bounds index:

    const products = ["laptop", "phone", "tablet", "monitor"];
    
    for (let i = 0; i < products.length; i++) {
      console.log(`${i}: ${products[i]}`);
    }
    // 0: laptop
    // 1: phone
    // 2: tablet
    // 3: monitor
    
    // Check before accessing to guard against empty arrays:
    if (products.length > 0) {
      console.log(`First item: ${products[0]}`);
    }
    

    Real-World Pattern: Progress Tracking

    const completedLessons = ["Variables", "Loops", "Functions"];
    const totalLessons = 10;
    
    const progress = (completedLessons.length / totalLessons) * 100;
    console.log(`Progress: ${progress}%`);   // Progress: 30%
    
    const remaining = totalLessons - completedLessons.length;
    console.log(`${remaining} lessons left`);  // 7 lessons left
    

    Common Mistake: Off-by-One

    const arr = [10, 20, 30];
    
    // WRONG — accesses index 3, which doesn't exist:
    console.log(arr[arr.length]);    // undefined
    
    // CORRECT — last element is at index length - 1:
    console.log(arr[arr.length - 1]);   // 30
    

    Key Takeaways

    • array.length is a property (no parentheses) — it returns the count of elements.
    • Last element: arr[arr.length - 1] or arr.at(-1) (ES2022).
    • Loop condition: i < arr.length (strict less-than keeps you in bounds).
    • length updates automatically when you push(), pop(), or otherwise modify the array.

    What to Learn Next

    With length solid, you're ready for for...of loops and forEach() — cleaner ways to iterate without managing index variables manually. Then explore array slicing with slice(start, end) to extract sub-arrays using length-relative positions, and array destructuring to pull out multiple elements in a single line.

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free