mc0de
JavaScript · Lesson 08 of 17

JavaScript Arrays

One variable, many values! Discover how arrays organize collections of information, help you work with lists, and make your websites more interactive.

Array Basics Indexes Array Methods Lists & Loops
● ● ●   arrays.js
const fruits = [
  "Apple",
  "Banana",
  "Orange"
];

console.log(fruits[0]);
ARRAY EXPLORER
> Ready to explore…
01 / Getting Started

What Is a JavaScript Array?

Imagine you’re creating a website that displays your favorite movies, a shopping list, or the names of students in a classroom. You could create a separate variable for every item, but managing all those variables would become difficult.

An array stores an ordered collection of values in a single variable. Each value is called an element.

Creating an array
const fruits = ["Apple", "Banana", "Orange"];

console.log(fruits);
Console output
[“Apple”, “Banana”, “Orange”]

Arrays use square brackets [], with commas separating the elements. They can hold strings, numbers, Booleans, objects, or even other arrays.

Remember: An array is useful whenever several related values belong together, especially when you’ll need to display, search, or update them.
02 / Array Indexes

Access Items by Their Position

Every element in an array has a numbered position called an index. JavaScript starts counting indexes at zero, not one.

Accessing array elements
const colors = ["Red", "Green", "Blue"];

console.log(colors[0]);
console.log(colors[1]);
console.log(colors[2]);
Console output
Red Green Blue

The first item is at index 0, the second at 1, and the third at 2.

You can also change an existing element by assigning a new value to its index.

Updating an element
const colors = ["Red", "Green", "Blue"];

colors[1] = "Yellow";

console.log(colors);
Console output
[“Red”, “Yellow”, “Blue”]

Even though the variable was declared using const, its array elements can still be changed. What const prevents is assigning an entirely different value to the variable itself.

03 / Array Length

Find Out How Many Items You Have

Arrays have a built-in length property that tells you how many positions they contain.

Checking array length
const animals = ["Cat", "Dog", "Bird"];

console.log(animals.length);
console.log(animals[animals.length - 1]);
Console output
3 Bird

Since array indexes start at zero, the last element in a nonempty array is located at length - 1.

Helpful shortcut: You can also use animals.at(-1) to get the last element in modern JavaScript.
04 / Adding & Removing Items

Make Your Arrays Dynamic

Arrays can grow and shrink as your application changes. JavaScript provides built-in methods for adding and removing elements.

Adding items
const tasks = ["Study", "Practice"];

tasks.push("Build a project");
tasks.unshift("Review notes");

console.log(tasks);
Console output
[“Review notes”, “Study”, “Practice”, “Build a project”]

push() adds an item to the end of an array, while unshift() adds an item to the beginning.

Removing items
const tasks = ["Study", "Practice", "Build"];

tasks.pop();
tasks.shift();

console.log(tasks);
Console output
[“Practice”]

pop() removes the last item, and shift() removes the first. Both methods also return the item that was removed.

05 / Looping Through Arrays

Work With Every Item Automatically

In Lesson 06, you learned how loops repeat instructions. Arrays and loops work especially well together because you can process every item without writing separate instructions for each one.

Using a for loop
const fruits = ["Apple", "Banana", "Orange"];

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
Console output
Apple Banana Orange

The counter begins at index zero and continues while it's smaller than the array's length.

Another readable option is the for...of loop, which gives you each value directly.

Using for...of
for (const fruit of fruits) {
  console.log("I like " + fruit);
}

This approach is particularly helpful when you don't need to know the index of each item.

06 / Useful Array Methods

Search, Transform & Filter Lists

JavaScript arrays include many helpful methods. Here are four you'll use frequently when building interactive websites.

Finding an item
const fruits = ["Apple", "Banana", "Orange"];

console.log(fruits.includes("Banana"));
console.log(fruits.indexOf("Orange"));
Console output
true 2

includes() checks whether a value exists, while indexOf() returns its first matching index, or -1 if it isn't found.

Transforming and filtering values
const numbers = [1, 2, 3, 4, 5];

const doubled = numbers.map(number => number * 2);
const even = numbers.filter(number => number % 2 === 0);

console.log(doubled);
console.log(even);
Console output
[2, 4, 6, 8, 10] [2, 4]

map() creates a new array by transforming each item. filter() creates a new array containing only items that pass a condition. Neither method changes the original array in these examples.

07 / Arrays in Real Websites

Turn Array Data Into Page Content

Arrays become especially useful when you connect them to HTML. A webpage can use an array as its source of information, then update the displayed list whenever the data changes.

Displaying an array on a webpage
const hobbies = ["Coding", "Reading", "Gaming"];
const list = document.getElementById("hobbyList");

for (const hobby of hobbies) {
  const item = document.createElement("li");
  item.textContent = hobby;
  list.appendChild(item);
}

With an HTML element such as <ul id="hobbyList"></ul>, the script creates one list item for every value in the array.

When you later add or remove items from the array, you can redraw the list to keep the webpage in sync with your data.

Good habit: When displaying text entered by visitors, use textContent instead of inserting that text as HTML. It displays the value without interpreting it as markup.
Try an Array Explorer

Click through the values to see how indexes work.

Click a button to explore the array.
🚀 mC0de Challenge

Build a Favorite Things List Manager

Create an interactive list where visitors can add their favorite things, see the list update immediately, and remove items they no longer want.

Your Mission

  1. Create an array named favorites with three starting items.
  2. Display every item in an HTML list using a loop.
  3. Add a text field where visitors can enter a new favorite thing.
  4. Use push() to add the new value to the array.
  5. Update the displayed list after each change.
  6. Add a button that removes the last item using pop().
  7. Display the total number of items using length.
Extra challenge: Add a Remove button next to each item. Use splice() to delete a specific element by its index, and prevent duplicate entries using includes().
💻 Interactive JavaScript Sandbox

Build Your List Manager!

Edit the array, customize the page, and run your code. The starter includes adding and removing the last item. Select Load Solution to see individual Remove buttons, duplicate prevention, and an item counter.

Live Result
Lesson 08 Recap

What You’ve Learned

You're Working With Collections!

You've learned how to organize related information and use arrays to power interactive webpage features.

  • Create arrays using square brackets.
  • Access and update elements using zero-based indexes.
  • Find the number of items using length.
  • Add and remove items with push(), pop(), shift(), and unshift().
  • Loop through arrays using for and for...of.
  • Search and transform collections with array methods.
  • Build an interactive list manager.

Next up: Objects — learn how to group related information into properties and build more realistic JavaScript applications!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to mastering JavaScript.

🎉

Nice Work!

You've learned how to organize information using JavaScript arrays, access elements by index, add and remove values, loop through collections, and use powerful array methods. You've even built your own interactive Favorite Things List Manager!