How to Get Array Length in JavaScript

Use the length property when you need the total number of items in an array. This is the fastest way to count elements for loops, validation, empty checks, pagination, and UI states.

What you’ll build or solve

You’ll learn how to get array length in JavaScript using the length property. You’ll also know how to use it for counting, empty checks, and last-item access.

When this approach works best

This approach is the right choice when logic depends on how many items exist.

Common real-world scenarios include:

  • Checking if a cart has items
  • Loop boundaries
  • Last element access
  • Pagination totals
  • Validation before rendering

This is a bad idea when you need the number of matching items after a condition. In that case, filter first, then use length.

Prerequisites

You only need:

  • A JavaScript file or browser console
  • A basic array
  • Basic JavaScript array knowledge

Step-by-step instructions

Step 1: Read the length property from the array

Use dot notation on the array variable.

JavaScript

const tasks = ["Write docs", "Fix bug", "Ship update"];

console.log(tasks.length);

You can also combine it with logic checks.

JavaScript

if (tasks.length === 0) {
  console.log("No tasks yet");
}

A common pattern is getting the last item.

JavaScript

const lastTask = tasks[tasks.length - 1];
console.log(lastTask);

What to look for:

  • length is a property, not a function
  • It returns the total item count
  • Empty arrays return 0
  • length - 1 gives the last index
  • Great for loops and empty states

Examples you can copy

Cart item count

JavaScript

const cart = ["Keyboard", "Mouse", "Monitor"];

console.log(cart.length);

Empty state check

JavaScript

const notifications = [];

if (notifications.length === 0) {
  console.log("All clear");
}

Last message

JavaScript

const messages = ["Hi", "Are you free?", "See you soon"];

const latest = messages[messages.length - 1];

Common mistakes and how to fix them

Mistake 1: Calling length like a function

What the reader might do:

JavaScript

console.log(tasks.length());

Why it breaks: length is a property, not a method.

Corrected approach:

JavaScript

console.log(tasks.length);

Mistake 2: Using length as the last index

What the reader might do:

JavaScript

const lastItem = tasks[tasks.length];

Why it breaks: array indexes start at 0, so this points one position past the end.

Corrected approach:

JavaScript

const lastItem = tasks[tasks.length - 1];

Mistake 3: Forgetting empty array checks

What the reader might do:

JavaScript

const latest = tasks[tasks.length - 1];

Why it breaks: empty arrays return undefined.

Corrected approach:

JavaScript

if (tasks.length > 0) {
  const latest = tasks[tasks.length - 1];
}

Troubleshooting

If length throws an error, confirm the variable is actually an array.

If the last item is undefined, subtract 1 from the length.

If empty states fail, compare against 0.

If filtered counts are needed, use filter() first, then read length.

Quick recap

  • Use .length to count array items
  • It is a property, not a function
  • Empty arrays return 0
  • Use length - 1 for the last item
  • Great for loops, checks, and UI counts