JS CodeLab

JavaScript Arrays Guide: Methods, Iteration & Manipulation

By JS CodeLab Engineering Team•Last updated: October 12, 2026•5 min read

Direct Answer & Key Concept

A JavaScript array is an ordered list of values that can store multiple data types under a single variable name. Arrays feature zero-based indexing and provide a rich set of built-in functional transformation methods.

Runnable Code SnippetRun in Playground
// Array Transformation Pipeline
const cartItems = [
  { item: "Laptop", price: 1200, qty: 1 },
  { item: "USB Hub", price: 25, qty: 2 },
  { item: "Mousepad", price: 15, qty: 3 }
];

// Calculate total cart value with reduce()
const subtotal = cartItems.reduce((acc, curr) => acc + curr.price * curr.qty, 0);

console.log("Subtotal:", "$" + subtotal); // Subtotal: $1295
console.log("Item count:", cartItems.length); // Item count: 3

What is an array in JavaScript?

A JavaScript array is an ordered, zero-indexed collection of elements capable of storing any data type—including numbers, strings, nested objects, functions, and other arrays. Unlike arrays in strictly typed systems with static memory allocation, JavaScript arrays are dynamic in size, resize automatically, and offer powerful functional iteration pipelines.


Essential JavaScript Array Methods Comparison

The table below organizes standard array methods by whether they mutate the original array in place or return a clean, new array:

| Method | Purpose | Mutates Original Array? | Returns | | :--- | :--- | :--- | :--- | | map() | Transform every item | No | New array of same length | | filter() | Keep items matching condition | No | New filtered array | | reduce() | Accumulate items into single value | No | Aggregated value (any type) | | find() | Find first matching element | No | First element or undefined | | findIndex() | Find index of first match | No | Index integer or -1 | | includes() | Check if value exists | No | Boolean (true / false) | | slice() | Extract sub-array by indices | No | New shallow copy array | | splice() | Add or remove items in place | Yes | Array of deleted elements | | sort() | Order elements in place | Yes | Reference to mutated array | | push() / pop()| Append / remove from end | Yes | New length / popped element |


When to use Functional Transformations (map, filter, reduce)

Modern JavaScript embraces immutable programming patterns where arrays are transformed rather than mutated:

1. Filtering and Mapping in Pipelines

You can chain array methods cleanly to process complex data sets without imperative for loops:

const products = [
  { id: 1, name: "Keyboard", price: 79, inStock: true },
  { id: 2, name: "Monitor", price: 299, inStock: false },
  { id: 3, name: "Mouse", price: 49, inStock: true }
];

const inStockTitles = products
  .filter(p => p.inStock)
  .map(p => p.name.toUpperCase());

console.log(inStockTitles); // ["KEYBOARD", "MOUSE"]

2. Safe Array Copying with Spread

To avoid unintended mutations when sorting or reversing arrays, create a shallow copy using the spread operator ([...arr]) or the newer ECMAScript toSorted() and toReversed() methods:

const numbers = [5, 1, 9, 3];
const sorted = [...numbers].sort((a, b) => a - b);
console.log("Original:", numbers); // [5, 1, 9, 3] (Untouched)
console.log("Sorted:", sorted);     // [1, 3, 5, 9]

Common Mistakes with JavaScript Arrays

  1. Default sort() behavior on numbers: [10, 5, 20].sort() sorts lexicographically (converting numbers to strings), resulting in [10, 20, 5]. Always pass an explicit comparison comparator: .sort((a, b) => a - b).
  2. Mutating state unintentionally with splice(): Using splice() directly modifies the input array in place, which causes subtle bugs in React components and state stores. Use slice() or filter() for immutable updates.
  3. Using delete on array items: Calling delete arr[1] removes the element but leaves an empty hole (empty / undefined) without adjusting the array length. Use filter or toSpliced instead.

Frequently Asked Questions About JavaScript Arrays: Methods, Iteration & Manipulation

Does array.map() modify the original array?

No. The map() method creates and returns a completely new array populated with the results of calling the provided callback on every element without altering the original array.

Why does [10, 5, 20].sort() sort incorrectly in JavaScript?

By default, the Array.prototype.sort() method converts elements into strings and orders them according to UTF-16 code units. To sort numbers numerically, you must supply a comparator: (a, b) => a - b.

What is the difference between slice() and splice()?

The slice() method returns a shallow copy of a portion of an array without modifying the original. In contrast, splice() mutates the original array in place by removing, replacing, or inserting elements.

How do you check if a value is an Array in JavaScript?

Use Array.isArray(value). Checking typeof value returns 'object' because arrays in JavaScript are specialized objects, making Array.isArray the only dependable test.

Related Runnable JavaScript Examples

Ready to test what you learned?

The best way to solidify your JavaScript skills is by writing and running code. Open our free online compiler and test your scripts with real-time console feedback.

Open JavaScript Compiler