JavaScript Arrays
One variable, many values! Discover how arrays organize collections of information, help you work with lists, and make your websites more interactive.
const fruits = [
"Apple",
"Banana",
"Orange"
];
console.log(fruits[0]);
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.
const fruits = ["Apple", "Banana", "Orange"];
console.log(fruits);
Arrays use square brackets [], with commas separating the elements. They can hold strings, numbers, Booleans, objects, or even other arrays.
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.
const colors = ["Red", "Green", "Blue"];
console.log(colors[0]);
console.log(colors[1]);
console.log(colors[2]);
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.
const colors = ["Red", "Green", "Blue"];
colors[1] = "Yellow";
console.log(colors);
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.
Find Out How Many Items You Have
Arrays have a built-in length property that tells you how many positions they contain.
const animals = ["Cat", "Dog", "Bird"];
console.log(animals.length);
console.log(animals[animals.length - 1]);
Since array indexes start at zero, the last element in a nonempty array is located at length - 1.
animals.at(-1) to get the last element in modern JavaScript.Make Your Arrays Dynamic
Arrays can grow and shrink as your application changes. JavaScript provides built-in methods for adding and removing elements.
const tasks = ["Study", "Practice"];
tasks.push("Build a project");
tasks.unshift("Review notes");
console.log(tasks);
push() adds an item to the end of an array, while unshift() adds an item to the beginning.
const tasks = ["Study", "Practice", "Build"];
tasks.pop();
tasks.shift();
console.log(tasks);
pop() removes the last item, and shift() removes the first. Both methods also return the item that was removed.
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.
const fruits = ["Apple", "Banana", "Orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
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.
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.
Search, Transform & Filter Lists
JavaScript arrays include many helpful methods. Here are four you'll use frequently when building interactive websites.
const fruits = ["Apple", "Banana", "Orange"];
console.log(fruits.includes("Banana"));
console.log(fruits.indexOf("Orange"));
includes() checks whether a value exists, while indexOf() returns its first matching index, or -1 if it isn't found.
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);
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.
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.
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.
textContent instead of inserting that text as HTML. It displays the value without interpreting it as markup.Click through the values to see how indexes work.
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
- Create an array named
favoriteswith three starting items. - Display every item in an HTML list using a loop.
- Add a text field where visitors can enter a new favorite thing.
- Use
push()to add the new value to the array. - Update the displayed list after each change.
- Add a button that removes the last item using
pop(). - Display the total number of items using
length.
splice() to delete a specific element by its index, and prevent duplicate entries using includes().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.
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(), andunshift(). - Loop through arrays using
forandfor...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 CompletedKeep 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!


