W3docs

JavaScript Arrays

Learn JavaScript arrays from the ground up: how to create them, add and remove elements, iterate, transform, sort, search, and apply best practices.

Introduction to JavaScript Arrays

A JavaScript array is an ordered, integer-indexed collection of values. Unlike objects, which store keyed properties in no guaranteed order, an array keeps its items in a specific sequence and lets you reach any item by its position (its index). Indexes start at 0, so the first element is at index 0, the second at index 1, and so on.

Arrays are one of the most-used data structures in the language because they can hold a mix of any value type — numbers, strings, booleans, objects, even other arrays — and they grow or shrink automatically as you add and remove items. This page covers how to create arrays, change their contents, loop over them, transform and search them, and the practices that keep array-heavy code readable.

Creating and Initializing Arrays

The most common way to create an array is the array literal syntax — a comma-separated list inside square brackets:

let fruits = ["Apple", "Banana", "Cherry"];

You can also use the new Array() constructor, though it is rarely used and has a confusing edge case: when you pass a single number, it sets the array's length rather than adding that number as an element.

javascript— editable

Because of that pitfall, prefer the literal syntax for everything except deliberately pre-sizing an array.

Accessing Elements and Length

Read or replace an element by its index, and read the total count with the length property. Assigning to an index beyond the current end extends the array.

javascript— editable

Adding and Removing Elements

Four methods cover the two ends of an array. Note which value each one returns — this is a common source of bugs.

  • push() — adds one or more elements to the end and returns the new length.
javascript— editable
  • pop() — removes the last element and returns that element.
javascript— editable
  • unshift() — adds one or more elements to the beginning and returns the new length.
javascript— editable
  • shift() — removes the first element and returns that element.
javascript— editable

Inserting and Removing in the Middle with splice()

push/pop/shift/unshift only touch the ends. To add, remove, or replace items anywhere, use splice(start, deleteCount, ...itemsToInsert). It changes the array in place and returns an array of the removed items.

javascript— editable

Copying a Section with slice()

slice(start, end) returns a shallow copy of part of the array without modifying the original. The end index is not included.

javascript— editable

Iterating Over Elements

There are several ways to loop over an array. Use a for...of loop or forEach() when you just want each value; reach for index-based loops only when you actually need the index. (See the loops chapter for the full set of loop forms.)

  • for...of — the cleanest way to read each value in order.
javascript— editable
  • forEach() — runs a callback once per element, receiving the value, index, and the array itself.
javascript— editable

Transforming Arrays

These methods return a new array (or value) instead of mutating the original, which makes them ideal for predictable, chainable data processing.

  • map() — builds a new array by transforming every element.
javascript— editable
  • filter() — builds a new array with only the elements that pass a test.
javascript— editable
  • reduce() — boils the whole array down to a single value by accumulating a result. It takes a callback (accumulator, current) and an initial value.
javascript— editable

Because map and filter each return an array, you can chain them:

javascript— editable

Searching Arrays

  • includes() — returns true/false for whether a value exists. indexOf() returns the value's index, or -1 if it is not found.
javascript— editable
  • find() and findIndex() — return the first element (or its index) that satisfies a test callback.
javascript— editable

Sorting Arrays

sort() orders the elements in place and returns the same array.

Warning

By default, sort() converts elements to strings and compares them lexicographically. For numbers this gives surprising results (for example 10 sorts before 2). Always pass a compare function (a, b) => a - b for numeric sorting.

javascript— editable

Converting Arrays to Strings

The join() method combines all elements into a single string, using the separator you pass (a comma by default).

javascript— editable
Info

This page covers the essentials. For the complete catalogue with every method signature, see JavaScript Array Methods.

Advanced Array Operations

Multi-Dimensional Arrays

Because an array can hold other arrays, you can model grids and matrices. Use two indexes — matrix[row][column] — to reach an inner value.

javascript— editable

The Spread Operator

The spread operator (...) expands an iterable — an array or string — into individual elements wherever multiple values are expected. It is the modern way to combine and copy arrays. (For the related collecting form, see rest parameters and spread syntax.)

  • Combining arrays:
javascript— editable
  • Copying an array (a shallow copy, so the original is untouched):
javascript— editable
  • Passing array items as function arguments:
javascript— editable

Array Destructuring

Destructuring unpacks array values into separate variables in one statement. (The same idea applies to objects — see destructuring assignment.)

javascript— editable
  • Swapping variables without a temporary variable:
javascript— editable
  • Default values fill in when a position is missing:
javascript— editable

Best Practices for Using Arrays

  • Declare with const when the array reference won't change. const only prevents reassigning the variable — you can still push, pop, and edit elements, because the array's contents are not frozen.
javascript— editable
  • Prefer map, filter, and reduce over manual loops for transforming data — they describe intent and return new arrays instead of mutating shared state.
  • Use the spread operator to copy before mutating when you want to avoid changing the original array.
  • Always pass a compare function to sort() for numbers.

Conclusion

Arrays are the backbone of data handling in JavaScript: ordered, index-based, and dynamically sized. Once you know which methods mutate the array (push, pop, splice, sort) versus which return new values (map, filter, slice, reduce), you can manipulate collections confidently. Combine that with the spread operator and destructuring for concise, readable code. Next, explore the full array methods reference and how arrays compare with JavaScript objects.

Practice

Practice
Which of the following are valid ways to create an array in JavaScript?
Which of the following are valid ways to create an array in JavaScript?
Practice
What does the pop() method return?
What does the pop() method return?
Practice
Why should you pass a compare function to sort() when sorting numbers?
Why should you pass a compare function to sort() when sorting numbers?
Was this page helpful?