JavaScript Objects for Beginners: A Simple Guide
What is an object in JavaScript? Learn how to create, read, update, and loop through objects with beginner-friendly examples and line-by-line explanations.
What is an object in JavaScript? Learn how to create, read, update, and loop through objects with beginner-friendly examples and line-by-line explanations.
What is an object in JavaScript? In plain words: an object is a way to group related values together under one name, using labels called "keys" to describe each value. If you have already learned about variables and arrays, objects are the next natural step - they let you model real things, like a person, a product, or a blog post, in a single, organized package.
In this guide you will learn what objects are, how to create them, how to read and change their values, how to add functions inside them, and how they compare to arrays. Every example is small and copy-pasteable, with a plain-English explanation right after it.
A JavaScript object is a collection of key-value pairs, wrapped in curly braces { }. Each "key" (also called a property name) points to a "value", which can be text, a number, true/false, an array, another object, or even a function.
const student = {
name: "Maria",
age: 21,
isEnrolled: true
};
console.log(student);Line by line: const student = { ... } creates a variable called student and stores an object inside it. name: "Maria" is a key-value pair - the key is name, and the value is the text "Maria". The next two lines add more key-value pairs, separated by commas. console.log(student) prints the whole object to the console so you can see it.
The curly-brace syntax you just saw is called an object literal, and it is the most common way to create an object in JavaScript. You do not need any special keyword - just write the keys and values inside { }.
const product = {
title: "Wireless Mouse",
price: 19.99,
inStock: true
};title, price, and inStock are the keys. "Wireless Mouse", 19.99, and true are their matching values. Notice that text values need quotes, but numbers and true/false do not. This object now represents one product, with all of its details grouped in one place instead of scattered across separate variables.
Once you have an object, you can read its values in two ways: dot notation (object.key) or bracket notation (object["key"]). Dot notation is shorter and is used most of the time.
console.log(product.title); // "Wireless Mouse"
console.log(product["price"]); // 19.99product.title reads the value stored at the title key using dot notation. product["price"] does the same thing using bracket notation, with the key written as a text string inside square brackets. Bracket notation is useful when the key name is stored in a variable, or when the key has spaces in it (like "first name").
Objects are not fixed once you create them. You can add new key-value pairs, change existing ones, or remove them completely.
product.discount = 5; // add a new property
product.price = 17.99; // update an existing property
delete product.inStock; // remove a property
console.log(product);
// { title: "Wireless Mouse", price: 17.99, discount: 5 }product.discount = 5 creates a brand new key called discount because it did not exist before. product.price = 17.99 overwrites the old price value with a new one. delete product.inStock removes the inStock key and its value entirely from the object. This flexibility is one of the main reasons objects are so useful.
A value inside an object can be a function. When a function lives inside an object, it is called a method. Methods let an object do something, not just store data.
const person = {
name: "Sam",
greet: function () {
return "Hello, my name is " + this.name;
}
};
console.log(person.greet()); // "Hello, my name is Sam"greet: function () { ... } stores a function as the value of the greet key. Inside the method, this.name refers to the name property of the same object - this always points back to the object the method belongs to. person.greet() calls the method, using parentheses just like calling any other function.
Sometimes you want to look at every key and value in an object without writing them out by hand. The for...in loop does exactly that.
const car = { brand: "Toyota", model: "Corolla", year: 2024 };
for (const key in car) {
console.log(key + ": " + car[key]);
}
// brand: Toyota
// model: Corolla
// year: 2024for (const key in car) runs the loop once for every key in the car object, storing the current key name in the key variable each time. car[key] uses bracket notation to look up the value that belongs to that key. This pattern is the standard way to inspect an object you did not build by hand, such as data returned from a server.
Beginners often mix up objects and arrays because both can hold multiple values. The difference comes down to how you look things up: arrays use numbered positions, objects use named keys.
| Array | Object | |
|---|---|---|
| Looks like | [10, 20, 30] | { a: 10, b: 20 } |
| Access a value by | Position/index, e.g. arr[0] | Key name, e.g. obj.a |
| Best for | An ordered list of similar items | A single thing with named properties |
| Example use case | A list of usernames | One user's profile (name, email, age) |
As a rule of thumb: if you are storing a list of similar items, use an array. If you are describing one thing with several different properties, use an object. In real projects you will often combine them - for example, an array of user objects.
A property's value can be another object. This lets you model more detailed, real-world data, like an address that lives inside a user.
const user = {
name: "Aisha",
address: {
city: "Nairobi",
country: "Kenya"
}
};
console.log(user.address.city); // "Nairobi"The address key holds its own object, with city and country inside it. To reach a value that is nested two levels deep, chain the dots together: user.address.city first goes into address, then reads city from there.
An object is a way to group related values together using named labels called keys, instead of separate variables. For example, a person's name, age, and email can all live inside one object, making the data easier to manage and pass around.
An array stores values in an ordered list and you access items by their numeric position, like arr[0]. An object stores values under named keys and you access them by name, like obj.name. Use an array for a list of similar items and an object for one item with several properties.
Use dot notation, such as object.propertyName, or bracket notation, such as object["propertyName"]. Dot notation is more common, but bracket notation is needed when the key has spaces or is stored in a variable.
Yes. A function stored inside an object is called a method. Methods let an object perform an action, such as person.greet(), and can use the this keyword to refer back to the object's own properties.
Use a for...in loop, which runs once for each key in the object. Inside the loop you can read the matching value with bracket notation, for example object[key], to print or process every property.
Already comfortable with objects? See how they fit inside JavaScript arrays, another must-know building block for beginners.
Read the JavaScript arrays guideWant help building a real project with JavaScript? Our team can guide you from your first script to a finished product.
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.