JavaScript Objects: Properties, Methods, and Object Literals

JavaScript objects are the main way to group related data and behavior into one value. If you want to store information about a user, a product, a settings panel, or almost any real-world thing in JavaScript, objects are usually the right starting point.

Quick answer: A JavaScript object is a collection of named properties, where each property has a key and a value. You create one with curly braces, read values with dot or bracket notation, and use methods when a property is a function.

Difficulty: Beginner

You'll understand this better if you know: basic variables, strings, numbers, arrays, and how functions return values.

1. What Is JavaScript Objects?

A JavaScript object is a value that stores related information as key-value pairs. The keys are usually strings, and the values can be almost anything: strings, numbers, arrays, other objects, or functions.

  • Objects let you model real-world things such as users, carts, and settings.
  • They are flexible: each property can hold a different type of value.
  • They are accessed by property name, not by numeric index like arrays.
  • They are reference values, which affects copying and comparison.

Objects are one of the most important data structures in JavaScript because they are used everywhere, from simple app state to browser APIs and JSON data.

2. Why JavaScript Objects Matter

Objects solve the problem of related data being scattered across many separate variables. Instead of keeping name, email, and age in unrelated variables, you can store them together in one object.

That makes code easier to read, easier to pass around, and easier to extend later. When your data grows, objects help you keep it organized without inventing a new variable for every field.

Objects also matter because JavaScript itself uses them heavily. Many built-in features, DOM elements, configuration values, and API responses use object-shaped data.

3. Basic Syntax

The simplest object uses an object literal, written with curly braces. Each property has a name, a colon, and a value.

Creating a basic object

This example creates a user object with three properties.

JavaScript
const user = {
  name: "Ava",
  age: 28,
  isAdmin: false
};

Here, name, age, and isAdmin are keys, and the values can be different types.

Reading properties

You can read a property with dot notation or bracket notation.

JavaScript
const name = user.name;
const age = user["age"];

Dot notation is shorter, but bracket notation is necessary when the property name is stored in a variable or contains characters that cannot be used with dots.

4. Step-by-Step Examples

Example 1: Updating a property

Objects are mutable, so you can change a property after creating the object.

JavaScript
const profile = {
  username: "mila",
  theme: "light"
};

profile.theme = "dark";

After this update, profile.theme is "dark".

Example 2: Nested objects

Objects can contain other objects. This is common for address data, API responses, and application settings.

JavaScript
const order = {
  id: 1001,
  customer: {
    name: "Noah",
    city: "Lagos"
  }
};

const city = order.customer.city;

This shows how object paths let you reach deeply nested data.

Example 3: Methods on objects

A method is just a function stored as an object property. Methods usually act on the object's own data.

JavaScript
const cart = {
  items: 3,
  total: 75,
  getSummary: function () {
    return `${this.items} items for $${this.total}`;
  }
};

When you call cart.getSummary(), the method can use this to refer to cart.

Example 4: Dynamic property names

Bracket notation is useful when the property name comes from a variable.

JavaScript
const field = "email";
const record = {
  [field]: "user@example.com"
};

This is a common pattern when building form data, filters, or API payloads.

5. Practical Use Cases

  • Representing a user profile with fields like name, email, and preferences.
  • Storing application settings such as theme, language, and notification options.
  • Modeling a product with price, stock, category, and discount state.
  • Returning structured data from functions instead of many separate values.
  • Working with JSON from APIs, which often maps directly to JavaScript objects.

Objects are especially useful when the data has named fields and the order of the fields is not the main concern.

6. Common Mistakes

Mistake 1: Using dot notation with an invalid property name

Dot notation only works with valid identifier names. If the property name has a space, starts with a number, or comes from a variable, you need bracket notation.

Problem: This code tries to use dot notation with a property name that JavaScript cannot read as a normal identifier.

JavaScript
const person = {
  "home city": "Berlin"
};

const city = person.home city;

Fix: Use bracket notation for property names that are not simple identifiers.

JavaScript
const person = {
  "home city": "Berlin"
};

const city = person["home city"];

The corrected version works because bracket notation can access any string key.

Mistake 2: Forgetting that objects are reference values

When you assign one object variable to another, you copy the reference, not the contents. Changing one variable then changes the same underlying object.

Problem: This code assumes copy is a separate object, but both variables point to the same object in memory.

JavaScript
const settings = { mode: "light" };
const copy = settings;

copy.mode = "dark";

console.log(settings.mode);
Output✓ Tested · Node 24
dark

Fix: Make a shallow copy when you need a separate object.

JavaScript
const settings = { mode: "light" };
const copy = { ...settings };

copy.mode = "dark";

console.log(settings.mode);
Output✓ Tested · Node 24
light

The corrected version works because spreading creates a new top-level object.

Mistake 3: Calling a missing method or reading a missing property without checks

If you assume a property exists when it does not, you can end up with undefined or a runtime error when you try to use it like a function.

Problem: This code calls user.getDisplayName(), but the object does not have that method, so JavaScript throws a runtime error.

JavaScript
const user = {
  name: "Lina"
};

user.getDisplayName();

Fix: Check that the method exists or define it on the object before calling it.

JavaScript
const user = {
  name: "Lina",
  getDisplayName() {
    return this.name;
  }
};

const label = user.getDisplayName();

The corrected version works because the method is actually part of the object.

7. Best Practices

Practice 1: Use objects for named data, not ordered lists

If the information is identified by names such as name or price, objects are clearer than arrays.

JavaScript
const book = {
  title: "Clean Code",
  author: "Robert C. Martin"
};

This is easier to understand than remembering what each array index means.

Practice 2: Prefer bracket notation when the key is dynamic

Dynamic keys are common in forms, filters, and configuration screens. Brackets make the code explicit and safe.

JavaScript
const key = "status";
const query = { [key]: "active" };

This avoids hardcoding the property name when the name may change.

Practice 3: Keep methods focused on the object's own data

Methods are easier to maintain when they work with the object they belong to instead of relying on outside state.

JavaScript
const invoice = {
  subtotal: 80,
  taxRate: 0.15,
  total() {
    return this.subtotal + this.subtotal * this.taxRate;
  }
};

Keeping the logic close to the data makes the object more reusable and easier to test.

8. Limitations and Edge Cases

  • Objects do not preserve a meaningful sort order for application logic, even though property enumeration follows specific rules.
  • Objects are shallow by default, so copying the top level does not clone nested objects.
  • Property names are strings or symbols internally, so two different-looking keys can still refer to the same string form.
  • Comparing two separate objects with === checks reference equality, not structural equality.
  • Arrays are objects too, but they are optimized for ordered numeric indexes and should usually be used for lists.

A common surprise is that two objects with the same contents are not equal unless they are the exact same reference.

9. Practical Mini Project

Let's build a small contact card object and use it to produce a readable profile summary. This example shows object creation, property access, nested data, and a method.

JavaScript
const contact = {
  firstName: "Sofia",
  lastName: "Patel",
  details: {
    email: "sofia@example.com",
    city: "Toronto"
  },
  getSummary() {
    return `${this.firstName} ${this.lastName} - ${this.details.city} - ${this.details.email}`;
  }
};

const summary = contact.getSummary();
console.log(summary);
Output✓ Tested · Node 24
Sofia Patel - Toronto - sofia@example.com

This object is small, but it shows the pattern you will use in larger programs: store related values together and expose behavior through methods.

10. Key Points

  • JavaScript objects store data as named properties.
  • Use dot notation for simple property names and bracket notation for dynamic or unusual names.
  • Objects can contain nested objects, arrays, and functions.
  • Objects are reference values, so assignment does not create a copy.
  • Methods are functions stored inside objects and often use this.

11. Practice Exercise

Create a playlist object with name, owner, and songs properties. Add a method named getInfo that returns a sentence like "Chill Vibes by Maya has 4 songs".

  • Use an object literal.
  • Store the songs as an array.
  • Make the method read the object's own properties.

Expected output: A single descriptive string based on the object's data.

Hint: Use this.name, this.owner, and this.songs.length.

Show solution
JavaScript
const playlist = {
  name: "Chill Vibes",
  owner: "Maya",
  songs: ["Song A", "Song B", "Song C", "Song D"],
  getInfo() {
    return `${this.name} by ${this.owner} has ${this.songs.length} songs`;
  }
};

console.log(playlist.getInfo());
Output✓ Tested · Node 24
Chill Vibes by Maya has 4 songs

12. Final Summary

JavaScript objects are the standard way to group related data and behavior into one value. They help you model real things, build clearer APIs, and organize application state with named properties instead of numeric positions.

To use objects well, remember the core patterns: create them with object literals, access properties with dot or bracket notation, define methods for behavior, and copy them carefully when you need independence. The more comfortable you are with objects, the easier it becomes to work with arrays of objects, JSON, and almost every modern JavaScript codebase.

Next, try learning object destructuring and Object.keys(), Object.values(), and Object.entries() so you can inspect and transform objects more effectively.