Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Arrays

Arrays store ordered lists of values and are one of the most used data structures in JavaScript.

Once you know the basics, methods like map, filter, and reduce become powerful tools.

Why We Need It

Most apps work with lists: products, users, messages, and more.

Arrays give you efficient ways to store, transform, and search that data.

Syntax

const items = [value1, value2, value3]
items[index]
items.length

Basic Example

1. Create and access

const fruits = ["apple", "banana", "mango"];

console.log(fruits[0]); // apple
console.log(fruits.length); // 3

Arrays are zero-indexed and have a length property.

Real World Example

2. Add and remove

const list = [1, 2];
list.push(3);
list.pop();

console.log(list); // [1, 2]

push and pop modify the array.

Multiple Use Cases

Store Ordered Data

Arrays hold ordered collections of values. Indexes start at 0 and go up from there.

They are perfect for lists of items, records, or any sequence you need to process.

  • Use arrays for ordered data.
  • Access items by index.
  • Arrays can hold mixed types.

Common Operations

You can add, remove, and update items using push, pop, shift, and splice.

You can also create new arrays with map, filter, and slice.

Iteration

Loop through arrays using for, for...of, or array methods like forEach.

Choose the style that is most readable for your use case.

Immutability vs Mutation

Some methods mutate the original array (push, pop, splice, sort).

Others return new arrays (map, filter, slice). Know which behavior you need.

More Examples

3. Map and filter

const nums = [1, 2, 3, 4];
const doubled = nums.map((n) => n * 2);
const evens = nums.filter((n) => n % 2 === 0);

console.log(doubled, evens);

map and filter return new arrays.

4. Reduce

const prices = [10, 20, 30];
const total = prices.reduce((sum, p) => sum + p, 0);

console.log(total);

reduce collapses an array into a single value.

Comparison

Without

const total = prices[0] + prices[1] + prices[2];

With

const total = prices.reduce((sum, p) => sum + p, 0);

Common Mistakes and Fixes

Off-by-one indexing

Remember arrays start at index 0.

Mutating when you need immutability

Use map, filter, or slice to create new arrays.

Using for...in on arrays

Prefer for...of or array methods for values.

Forgetting that sort mutates

Copy the array first if you need to preserve the original.

Interview Questions

What is an array?

An ordered list of values accessed by index.

How do you add items to an array?

Use push, unshift, or spread to create a new array.

Which methods do not mutate?

map, filter, and slice return new arrays.

Practice Problem

Practice: Given an array of numbers, return a new array with only odd numbers.

const nums = [1, 2, 3, 4, 5];
// TODO: filter odd numbers

One Possible Solution

const nums = [1, 2, 3, 4, 5];
const odds = nums.filter((n) => n % 2 === 1);
console.log(odds);

Frequently Asked Questions

Are arrays objects in JavaScript?

Yes, arrays are a special kind of object with indexed elements.

What is the fastest way to loop?

Performance is similar; choose the most readable approach.

How do I copy an array?

Use slice() or spread: const copy = [...arr].

Try It Yourself

Try changing the array values and indexes.