HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Objects

Objects

Storing data with key-value pairs, accessing and adding properties.

Simple enough for a 5-year-old to understand

Create and access object

An object holds the properties of an entity (key: value). The value is accessed with a dot.

Example code
1
const person = { name: "Alex", age: 36 };
Object with two properties.
DataObject with two properties.
2
console.log(person.name);
"name" property: Alex
Output / Input"name" property: Alex
3
console.log(person.age);
"age" property: 36
Output / Input"age" property: 36
DataOutput / Input
💡 Hint: An object does not have an order/number; you access the value by its key name (object.key).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How do you access name in the const k = { name: "Alex" } object?

About this topic

An object is a structure that stores data as key-value pairs. Think of a user: their name, email, age. Instead of keeping these in three separate variables, you group them into a single object and pass them around together.

In JavaScript, the object is a central concept: arrays, functions, and even classes are actually objects. JSON data coming from the server turns directly into an object; therefore, working with objects in web development is inevitable.

Objects are passed by reference. When you assign an object to another variable, it is not copied; the same object is simply given a second name. This is the most surprising behavior in JavaScript and the main source of unexpected side effects.

What you will learn

  • Creating objects and accessing properties
  • The difference between dot and bracket notation
  • Adding, updating, and deleting properties
  • Iterating with Object.keys, values, and entries
  • Unpacking properties into variables using destructuring assignment
  • That object copying is shallow

When to use?

You use objects to represent an entity: a user, a product, a set of settings. Passing a single options object to functions instead of numerous parameters is also a common and readable pattern.

If property names are determined at runtime or if you will be frequently adding and deleting them, the Map structure is more suitable than an object.

Common mistakes

Wrong

const a = { count: 1 };
const b = a;
b.count = 99;
console.log(a.count);

Objects are assigned by reference; b and a point to the same object, and the change is visible in both. The output is 99. If you want an independent copy, you must explicitly copy it.

Correct

const a = { count: 1 };
const b = { ...a };
b.count = 99;
console.log(a.count);

Wrong

const key = "name";
const user = { name: "Ada" };
console.log(user.key);

Dot notation treats the name you write literally as the property name; it does not use the value of the variable. The result is undefined. To access using a property name held in a variable, bracket notation is required.

Correct

const key = "name";
const user = { name: "Ada" };
console.log(user[key]);

Wrong

const config = { db: { host: "localhost" } };
const copy = { ...config };
copy.db.host = "remote";
console.log(config.db.host);

The spread operator produces a SHALLOW copy; nested objects are still shared. A change you make through the copy will also be visible in the original object, and the output will be "remote".

Correct

const config = { db: { host: "localhost" } };
const copy = structuredClone(config);
copy.db.host = "remote";
console.log(config.db.host);

Frequently asked questions

Should I use dot or bracket notation?

If you are hardcoding the property name in your code, dot notation is more readable. If the property name is held in a variable, or contains spaces or hyphens, bracket notation is mandatory. Bracket notation is used when working with dynamic property names coming from the server.

How do I check if a property exists in an object?

With the in operator or Object.hasOwn. Checking whether its value is undefined is insufficient, because a property might exist and its value could be undefined. If the distinction matters, using hasOwn is the correct approach.

How do I compare objects?

Triple equals compares objects by their references rather than their contents; two separate objects carrying the same properties are not considered equal. For content comparison, you need to check the properties one by one or use a helper library.

Summary

An object represents an entity with key-value pairs and is the natural counterpart of JSON data. Its critical behavior is that it is passed by reference: assignment does not copy, and the spread operator only produces a shallow copy.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code