Back to Blog
JavaScript

JavaScript Arrays: Your Friendly Guide to Taming Lists

9/7/2025
5 min read
JavaScript Arrays: Your Friendly Guide to Taming Lists

Feeling overwhelmed by JavaScript arrays? This friendly guide breaks them down into simple, human terms. Learn how to create, manipulate, and master arrays with practical examples.

JavaScript Arrays: Your Friendly Guide to Taming Lists

JavaScript Arrays: Your Friendly Guide to Taming Lists

JavaScript Arrays: Your Friendly Guide to Taming Lists

Let’s talk about one of the most common, useful, and sometimes confusing concepts in JavaScript: the Array.

If you’ve ever written a line of code, you’ve almost certainly needed a way to keep a list of things. A list of users, a list of to-do items, a list of pizza toppings you’ve selected (and then regretfully need to remove the anchovies).

In the real world, we manage lists all the time. JavaScript arrays are simply how we manage those lists inside our code. They’re not some scary, complex computer science concept. Think of them like a bookshelf—a neat way to organize and store your books (data) so you can find them, add new ones, or remove old ones easily.

So, What Exactly Is an Array?

At its heart, an array is an ordered list of values. Those values can be anything: numbers, strings, objects, even other arrays (mind-blowing, I know!). The key is that they are ordered, meaning each item has a specific position, known as its index.

And here’s the first "gotcha" that trips everyone up: array indexes start at 0, not 1.

It’s like standing in line. The first person is at position 0, the second at position 1, and so on. It’s a little weird at first, but you’ll get used to it, I promise.

How to Create Your First Array

Let’s stop talking and start making some arrays. The simplest way is using square brackets [].

javascript

// An empty array, like an empty shopping list
let shoppingList = [];

// An array of strings
let favouriteFoods = ['Pizza', 'Tacos', 'Ice Cream'];

// An array of numbers
let primeNumbers = [2, 3, 5, 7, 11];

// A mixed array (yes, JavaScript allows this!)
let randomStuff = [42, 'Hello', true, null];

Playing with Your Data: Adding and Removing Items

A static list is boring. The real power comes from manipulating the list. JavaScript gives us a ton of friendly methods for this. Let's look at the classics.

Adding Items:

  • .push(item): Adds to the end of the array. (Like adding an item to the end of your line.)

  • .unshift(item): Adds to the beginning of the array. (Like letting someone cut to the front of the line.)

Removing Items:

  • .pop(): Removes the last item and returns it. (Like the person at the end of the line leaving.)

  • .shift(): Removes the first item and returns it. (Like the first person in line leaving.)

javascript

let chores = ['Laundry', 'Dishes'];

chores.push('Vacuuming'); // Adds 'Vacuuming' to the end
// chores is now ['Laundry', 'Dishes', 'Vacuuming']

let done = chores.pop(); // Removes 'Vacuuming' and stores it in `done`
// chores is back to ['Laundry', 'Dishes']
console.log(done); // Output: 'Vacuuming'

Finding Your Way Around: Accessing and Finding Items

Remember the index? We use it to grab specific items.

javascript

let colours = ['Red', 'Green', 'Blue'];

console.log(colours[0]); // Output: 'Red' (the first item!)
console.log(colours[2]); // Output: 'Blue'

But what if the array is huge? You don't want to count items manually. That's where .find() and .indexOf() come in handy.

javascript

let users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];

// Find the user with the name 'Bob'
let bob = users.find(user => user.name === 'Bob');
console.log(bob); // Output: { id: 2, name: 'Bob' }

// Find the *position* of 'Green' in the colours array
let position = colours.indexOf('Green');
console.log(position); // Output: 1

The Superpower: Looping Through Arrays

What good is a list if you can't go through each item one by one? The most common way is with the .forEach() method. It's like saying, "For each thing in this list, do this."

javascript

let animals = ['Dog', 'Cat', 'Rabbit'];

animals.forEach(function(animal) {
  console.log(animal + ' is a great pet!');
});

// Output:
// Dog is a great pet!
// Cat is a great pet!
// Rabbit is a great pet!

Related Articles