JavaScript Arrays and Objects
Store lists in arrays and labelled data in objects, read and change them, and combine both for real data like a cart. After this, you can hold more than one value without twenty variables.
Course content
Sign in to save your progress.
-
Before You Write Code
-
What JavaScript Actually Does in Your Browser
-
Running JavaScript in the Console and in a Script File
-
-
-
The Basics You Cannot Skip
-
JavaScript let, const, and Why Not var
-
JavaScript Data Types and typeof
-
JavaScript == vs ===: Comparing Values Without Surprises
-
-
-
Making Decisions and Repeating Work
-
JavaScript if, else and the Ternary Operator
-
JavaScript Loops: for, for...of and while
-
-
-
Organizing Your Code
-
JavaScript Functions and Arrow Functions
-
JavaScript Arrays and Objects
-
-
-
Touching the Page
-
Changing the Page With the JavaScript DOM
-
JavaScript Click and Form Events
-
Reading JavaScript Errors in the Browser Console
-
-
- Final Check: JavaScript for Beginners
Skip ahead: If you can read and change items in an array of objects, like cart[0].qty, skip to "Copying: the mistake that changes the original".
One variable holds one value. Real data isn't like that. A cart has many items. Each item has a name, a price and a quantity. Arrays hold lists. Objects hold labelled data. Put them together and you can describe almost anything a web page needs.
Arrays: ordered lists
const items = ["rice", "eggs", "coffee"];
console.log(items[0]);
console.log(items.length);
items.push("sugar");
console.log(items);
console.log(items.includes("eggs"));
rice
3
[ 'rice', 'eggs', 'coffee', 'sugar' ]
true
- Positions start at 0. The last item is
items[items.length - 1]. pushadds to the end.popremoves from the end.includestells you if a value is in the list.
Reading a position that doesn't exist, like items[10], doesn't raise an error. You get undefined. That's where many "Cannot read properties of undefined" errors start.
Objects: labelled values
const product = {
name: "Coffee",
price: 95,
inStock: true,
};
console.log(product.name);
product.price = 99;
product.category = "Drinks";
console.log(product);
Coffee
{ name: 'Coffee', price: 99, inStock: true, category: 'Drinks' }
Each entry is a key and a value. Read with a dot: product.price. Change or add the same way. If the key is in a variable, use brackets: product[field].
Use an array when order matters and items are the same kind of thing. Use an object when each value has a different meaning and you want to look it up by name.
Together: an array of objects
This is the shape you'll see in almost every real project, and in most data that comes from a server.
const cart = [
{ name: "Coffee", price: 95, qty: 2 },
{ name: "Ensaymada", price: 35, qty: 6 },
];
let total = 0;
for (const item of cart) {
total = total + item.price * item.qty;
}
console.log("Items:", cart.length, "Total:", total);
console.log("First item:", cart[0].name);
Items: 2 Total: 400
First item: Coffee
Read cart[0].name from left to right: the cart, the first item, its name.
Finding something
find returns the first item that matches, or undefined if nothing does:
const cart = [
{ name: "Coffee", price: 95, qty: 2 },
{ name: "Ensaymada", price: 35, qty: 6 },
];
const tea = cart.find(item => item.name === "Tea");
console.log(tea);
if (tea) {
console.log(tea.price);
} else {
console.log("Tea is not in the cart");
}
undefined
Tea is not in the cart
⚠ Warning: Always handle the "not found" case. cart.find(...).price works while the item exists and crashes with Cannot read properties of undefined (reading 'price') the first time it doesn't. Check the result before you use it.
Copying: the mistake that changes the original
Assigning an array or object to another name does not copy it. Both names point to the same thing.
const original = ["rice", "eggs"];
const sameList = original;
sameList.push("coffee");
console.log(original);
const copy = [...original];
copy.push("sugar");
console.log(original.length, copy.length);
[ 'rice', 'eggs', 'coffee' ]
3 4
[...original] makes a new array with the same items. For objects, { ...product } does the same. Both are shallow copies: objects inside are still shared. That's enough for now.
💡 Tip: When an array or object prints in the browser console, click the small arrow to expand it. It shows every key and position, which is faster than logging each one.
Common mistakes
- Starting counting at 1:
items[1]is the second item. - Using a missing key and getting
undefinedwith no error:product.Priceis notproduct.price. - Thinking
constmakes an array read-only. It doesn't. - Changing a "copy" that isn't a copy.
What happens if you skip this
You'll end up with item1Name, item1Price, item2Name, item2Price, and a page that breaks the day someone buys three things.
How would you store a list of students, each with a name and three quiz scores? Sketch the shape before you write any code.
About
Store lists in arrays and labelled data in objects, read and change them, and combine both for real data like a cart. After this, you can hold more than one value without twenty variables.
- Duration
- 5 min
- Last update
- 09/02/2026
Comments (0)
Sign in to commentNo comments yet.