JavaScript Arrays for Beginners: A Simple Guide
Learn JavaScript arrays for beginners: how to create, read, update, and loop through arrays with simple, copy-paste code examples explained line by line.
Learn JavaScript arrays for beginners: how to create, read, update, and loop through arrays with simple, copy-paste code examples explained line by line.
If you searched for "JavaScript arrays for beginners", here is the short answer: an array is a single variable that holds a list of values, like a shopping list in one box instead of many separate boxes. In this guide you will learn how to create an array, read and change its items, loop through it, and use the most common array methods — with small code examples you can copy, paste, and run right away.
A variable normally holds one value, like a single name or a single number. An array is different: it holds many values, in order, inside one variable. Each value in the array is called an "element", and each element has a position called an "index". JavaScript starts counting positions at 0, not 1, so the first element is at index 0, the second is at index 1, and so on.
Arrays are useful whenever you have a list of things: names of students, prices in a cart, colors in a menu, or lines in a to-do list. Instead of creating a separate variable for every item, you put them all in one array.
const fruits = ["apple", "banana", "mango"];
const numbers = [10, 20, 30, 40];
const empty = [];
console.log(fruits);
console.log(numbers);Square brackets `[ ]` create an array. Inside the brackets, you list the values separated by commas. `fruits` is an array of three strings, `numbers` is an array of four numbers, and `empty` is an array with no items yet — you can add items to it later. We use `const` because we are not replacing the whole array, only changing what is inside it, which JavaScript still allows.
const fruits = ["apple", "banana", "mango"];
console.log(fruits[0]); // "apple"
console.log(fruits[1]); // "banana"
console.log(fruits[2]); // "mango"
console.log(fruits[3]); // undefinedTo read one item from an array, write the array name followed by the index in square brackets. `fruits[0]` gives you the first item, `fruits[1]` gives you the second, and so on. Asking for an index that does not exist, like `fruits[3]` in a 3-item array, does not cause an error — JavaScript simply returns `undefined`.
const fruits = ["apple", "banana", "mango"];
console.log(fruits.length); // 3
console.log(fruits[fruits.length - 1]); // "mango", the last item`.length` tells you how many items are in the array. It is very useful for loops, and also for getting the last item: since indexes start at 0, the last item is always at `length - 1`.
const fruits = ["apple", "banana", "mango"];
fruits[1] = "blueberry"; // change an item
fruits.push("orange"); // add to the end
fruits.unshift("grape"); // add to the start
fruits.pop(); // remove the last item
console.log(fruits); // ["grape", "apple", "blueberry", "banana"]You can change one item by assigning a new value to its index, like `fruits[1] = "blueberry"`. To add items, use `push()` to add one to the end, or `unshift()` to add one to the start. To remove items, use `pop()` to remove the last item, or `shift()` to remove the first item. These four methods change the original array directly.
Most of the time, you do not read array items one by one — you loop through the whole array and do something with every item. Here are the three most common ways beginners see.
const fruits = ["apple", "banana", "mango"];
// 1. classic for loop
for (let i = 0; i < fruits.length; i++) {
console.log(i, fruits[i]);
}
// 2. for...of loop
for (const fruit of fruits) {
console.log(fruit);
}
// 3. forEach method
fruits.forEach(function (fruit, index) {
console.log(index, fruit);
});The classic `for` loop counts from `i = 0` up to `fruits.length - 1`, using `i` as the index to read each item — this gives you full control, including the index number. The `for...of` loop is shorter: it hands you each value directly, without needing an index. `forEach()` is an array method that runs a function once for every item, and it also gives you the index as a second value if you need it. All three do the same job; `for...of` and `forEach()` are usually easier to read for beginners.
Once looping feels comfortable, two methods are worth learning early because they solve very common problems: `map()` builds a new array by changing every item, and `filter()` builds a new array that keeps only the items that pass a test.
const numbers = [1, 2, 3, 4, 5, 6];
const doubled = numbers.map(function (n) {
return n * 2;
});
console.log(doubled); // [2, 4, 6, 8, 10, 12]
const evens = numbers.filter(function (n) {
return n % 2 === 0;
});
console.log(evens); // [2, 4, 6]`map()` takes a function and applies it to every item, then returns a brand-new array with the results — here it multiplies every number by 2. `filter()` also takes a function, but that function must return `true` or `false`; only items where it returns `true` are kept — here `n % 2 === 0` checks if a number divides evenly by 2, so only even numbers survive. Neither method changes the original `numbers` array.
| Method | What it does | Changes the original array? |
|---|---|---|
| push(item) | Adds an item to the end | Yes |
| pop() | Removes the last item | Yes |
| unshift(item) | Adds an item to the start | Yes |
| shift() | Removes the first item | Yes |
| map(fn) | Builds a new array by transforming each item | No |
| filter(fn) | Builds a new array with items that pass a test | No |
| includes(item) | Checks if an item exists (true/false) | No |
| indexOf(item) | Finds the index of an item (-1 if missing) | No |
New to JavaScript? Start with the basics of variables and data types before diving deeper into arrays.
Read: JavaScript Variables and Data Types for Beginnersconst cart = ["notebook", "pen", "eraser"];
function printCart(items) {
items.forEach(function (item, index) {
console.log(`${index + 1}. ${item}`);
});
}
cart.push("ruler");
printCart(cart);
// 1. notebook
// 2. pen
// 3. eraser
// 4. rulerThis example combines everything from this guide. `cart` is an array of strings. `printCart` is a function that takes an array and loops through it with `forEach()`, printing a numbered list — `index + 1` is used because indexes start at 0 but people usually count from 1. Before printing, `push("ruler")` adds a fourth item to the end of the array. If you are new to writing functions like `printCart`, our guide on JavaScript functions covers that from scratch.
An array is a single variable that stores an ordered list of values, such as numbers or strings. You create one with square brackets, like `const colors = ["red", "blue"]`, and access each value using its index, starting at 0.
The easiest ways for beginners are `for...of`, which gives you each value directly, and `.forEach()`, which runs a function on every item. The classic `for` loop also works and gives you the index number if you need it.
An array stores an ordered list of values accessed by number, like `list[0]`. An object stores values as named properties accessed by key, like `person.name`. Use an array when order matters and items are similar; use an object when you need labeled fields.
No. Both `map()` and `filter()` return a brand-new array and leave the original array untouched. Methods that do change the original array include `push()`, `pop()`, `shift()`, `unshift()`, and `splice()`.
JavaScript does not check array bounds strictly. Asking for an index that does not exist simply returns the special value `undefined` instead of stopping your program, so it is good practice to check `array.length` before assuming an index exists.
Want help building a real project with JavaScript, from a simple script to a full web app?
Talk to the Fepiq teamOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
What is an index in SQL? A plain-English guide with copy-paste examples showing how indexes speed up queries and when you actually need one.
Learn JavaScript DOM manipulation for beginners: select elements, change text and styles, and handle clicks with simple, copy-paste code examples.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.