Objects: Named Bundles of Data

πŸ“Œ Concept 11 of 13✍️ Written by Tom ReyesπŸ“… Reviewed 2026-04-19⏱ ~13 min read

An object groups related data under named fields: a user has a name, an age, an email; a product has a title, a price, an inventory count. Objects are everywhere.

Bee, our debugging mascot

From Bee: Spread is shallow. {...obj} copies the top level only. Nested objects are still shared references. Use structuredClone for true copies.

What Is an Object?

An object (called a dictionary in Python and a HashMap in Java) groups related values under named keys. Where arrays answer "which one?" by position, objects answer by name.

Reading and Updating Fields

Two notations: dot notation (user.name) and bracket notation (user["name"]). Dot is shorter; brackets work when the key is dynamic.

Nested Objects

Object fields can themselves be objects or arrays. Real-world data is usually deeply nested: a user has an address; an address has a city; a city has a country.

Code Examples in Three Languages

let user = {
  name: "Aisha",
  age: 30,
  email: "a@example.com",
  address: { city: "Cairo", country: "EG" }
};

user.name;              // "Aisha"
user["age"];            // 30
user.address.city;      // "Cairo"

// Update / add
user.age = 31;
user.role = "admin";

// Iterate
for (const key in user) {
  console.log(key, user[key]);
}
user = {
    "name": "Aisha",
    "age": 30,
    "email": "a@example.com",
    "address": {"city": "Cairo", "country": "EG"}
}

user["name"]              # "Aisha"
user["address"]["city"]   # "Cairo"

# Update / add
user["age"] = 31
user["role"] = "admin"

# Iterate
for key, value in user.items():
    print(key, value)
// Java has no dict literal β€” use HashMap
Map<String, Object> user = new HashMap<>();
user.put("name", "Aisha");
user.put("age", 30);

user.get("name");      // "Aisha"
user.put("role", "admin");

for (Map.Entry<String, Object> e : user.entrySet()) {
    System.out.println(e.getKey() + ": " + e.getValue());
}

Best Practices

  1. Use objects when fields have names; use arrays when you have many of the same kind.
  2. Keep nesting shallow β€” flat data is easier to work with.
  3. Use consistent key naming across your app (camelCase or snake_case, pick one).
  4. Validate keys exist before reading β€” missing keys cause subtle bugs.

Common Mistakes

  • Reading missing keys. Returns undefined in JS, raises KeyError in Python.
  • Mixing key styles. user.firstName here, user.first_name there.
  • Mutating a shared object. Two parts of the code edit the same object and step on each other.
  • Deep clone confusion. const copy = obj doesn't copy β€” both names point to the same object.
πŸ›
See the bugs in action

We have a dedicated Common Bugs with Objects page β€” five real broken snippets with the fix. Read it before you start writing.

How It Works Under the Hood

A JavaScript object is roughly a hash map: keys are strings (or Symbols), values are any. Reading user.name hashes the key and looks up the slot β€” constant time on average. Internally V8 uses "hidden classes" to speed up access patterns, which is why creating objects with the same shape (always the same keys in the same order) is faster than dynamically adding fields.

Python dicts and Java HashMaps work similarly. The trade-off is the same: very fast lookup, no order guarantee historically (Python 3.7+ preserves insertion order, JS preserves order for string keys).

Objects are passed by reference, not value. const a = {}; const b = a; creates two names for one object β€” modifying b.x shows up in a.x. This is the source of more beginner bugs than perhaps any other concept.

From Beginner to Pro

The same concept gets deeper as you grow. Here's what mastery looks like at three levels:

Beginner

You read user.name with dot notation, set fields, iterate with Object.keys.

Intermediate

You destructure (const { name, age } = user), spread ({...user, role: "admin"}), and serialize to JSON.

Pro

You design data shapes deliberately, prefer immutable updates for shared state (Redux-style), use Map/Set when keys aren't strings, and serialize/deserialize at boundaries.

Performance & Gotchas

  • Reference sharing β€” const b = a doesn't copy.
  • Spread is shallow β€” nested values still share references.
  • JSON loses functions, undefined, dates β€” be explicit about what serializes.
  • Missing-key reads β€” undefined silently in JS, KeyError in Python.

Quick Quiz

Real-World Uses (Production Code, Today)

User profile

Every "user" in every app you've used is an object: {id, name, email, createdAt, settings: {...}}. Add a field β€” you've added a profile feature.

JSON APIs

Every API response is a tree of objects. fetch().then(r => r.json()) hands you exactly the structure from this lesson.

Configuration

App configs (Webpack, ESLint, package.json) are giant objects. Tuning your tools = editing object fields.

Frequently Asked Questions

What's the difference between an object and a class?

An object is a single instance with data. A class is a template/blueprint for creating objects. We cover OOP separately.

Can an object have a function as a value?

Yes β€” that field is then called a method.

How do I check if a key exists?

"name" in user in JS/Python; user.containsKey("name") in Java.

How do I delete a key?

delete user.name in JS, del user["name"] in Python, user.remove("name") in Java.

What's JSON?

A text format for objects/arrays. Looks almost identical to JS object syntax but with stricter rules (keys must be quoted strings).

How do I copy an object without sharing the reference?

{...obj} (JS) for shallow copy, structuredClone(obj) for deep. Python: copy.deepcopy(obj).

Can objects be nested?

Yes, arbitrarily deep. Use carefully β€” deep nesting is hard to read.

What's the difference between a dict and an object in Python?

Practically synonyms in everyday use; technically Python has both, with subtle differences (objects support methods/inheritance).

βœ…
Key takeaways
  • Objects: Named Bundles of Data is one of the 13 universal concepts of programming.
  • The syntax differs across languages, but the underlying idea is the same.
  • Practice in the playground to make it stick.
Was this helpful?
T
Tom Reyes
Reviewer Β· 12 yrs Java/JVM

Tom spent eight years as a backend engineer in fintech (Java + Kotlin) and four as a lead at an enterprise SaaS company. He reviews every Java example on this site and writes the data-structure deep dives. He cares deeply that beginners aren't taught bad habits they'll have to unlearn. More about Tom β†’