# Arrays: Create, Mutate & Access

> Master the fundamental data structure of the web. Learn how to store ordered lists, access data, and distinguish between mutating and non-mutating methods.

Subject: JavaScript | Lesson 26 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/arrays-fundamentals
Previous: [this in Classes & Method Binding](https://codewithanimation.in/javascript/this-in-classes.md) | Next: [Array Iteration: map, filter, reduce](https://codewithanimation.in/javascript/array-iteration-methods.md)

## The Array Data Structure

An Array is an ordered list of values. Unlike Objects, which use string keys to identify data, Arrays use numbered indexes.

Arrays are created using square brackets `[]`, and their indexes always start counting at zero (0).

## Mutating Methods (Destructive)

JavaScript provides several built-in methods that permanently change (mutate) the original array.

`push(val)` adds to the end, while `pop()` removes from the end.

`unshift(val)` adds to the beginning, while `shift()` removes from the beginning.

`splice(start, deleteCount, ...items)` is the ultimate mutator, capable of adding, removing, or replacing items anywhere in the array.

## Non-Mutating Methods (Safe)

Some methods deliberately avoid changing the original array. Instead, they calculate a result and return a brand **new array**.

The most common is `slice(start, end)`. It extracts a section of an array and returns it as a new copy, leaving the original perfectly intact.

`concat(arr)` merges two arrays together, returning a new, combined array.

## Common mistake: Accidental Mutations

Because arrays are Reference Types (just like objects), if you pass an array to a function and that function uses a mutating method like `splice()`, it alters the array everywhere in your app! Always be cautious when using destructive methods.

## Tip: The length property

Every array has an automatic `.length` property that tells you how many items are inside. The last item is always at `arr[arr.length - 1]`.

## Try it: The Array Workshop

Experiment with mutating methods (splice) versus non-mutating methods (slice) to see exactly how they impact the original data.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/arrays-fundamentals

## Check yourself

### Question 1

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

- A. splice() modifies the original array, slice() returns a new copy.
- B. slice() modifies the original array, splice() returns a new copy.
- C. There is no difference, they are aliases for the same function.

Correct answer: A

`splice()` is destructive (it alters the original array), whereas `slice()` is safe (it creates a brand new copy).

- Why not B: Incorrect. You have them backward.
- Why not C: Incorrect. They behave fundamentally differently.

### Question 2

If an array has 5 items, what is the index of the last item?

- A. 4
- B. 5
- C. 6

Correct answer: A

Because JavaScript arrays start counting at 0, the last item is always located at `length - 1`.

- Why not B: Incorrect. Arrays are zero-indexed, meaning the first item is 0.
- Why not C: Incorrect. Array indexes start at 0, not 2.

### Question 3

Which method adds a new item to the very beginning of an array?

- A. unshift()
- B. shift()
- C. push()

Correct answer: A

`unshift()` pushes all existing elements forward and inserts the new item at index 0.

- Why not B: Incorrect. `shift()` removes from the beginning.
- Why not C: Incorrect. `push()` adds to the end.

## Remember this

- Arrays are ordered collections accessed via zero-based numeric indexes.
- `push()`, `pop()`, `shift()`, `unshift()`, and `splice()` permanently mutate the array.
- `slice()` and `concat()` are non-mutating; they return a brand new copy.
- The `.length` property automatically updates as you add or remove items.
- Because arrays are objects, assigning them to new variables only copies the reference pointer, not the data.
