Skip to main content

Command Palette

Search for a command to run...

Objects in JavaScript: A Complete Beginner-Friendly Guide

Published
4 min readView as Markdown

Objects

Objects are one of the most important parts of JavaScript.
They let you store related information together and describe real-world things in code.

In this article, you will learn:

  • what objects are

  • how to create them

  • how to access and update properties

  • how to add methods

  • how nested objects work

  • how to loop through objects

  • common mistakes

  • best practices

Let’s begin.


What Is an Object?

An object stores data in key–value pairs.

Each key is called a property name, and each value can be anything:

  • number

  • string

  • boolean

  • array

  • another object

  • or even a function

Here is a simple example:

const person = {
  name: "John",
  age: 25,
  isStudent: false
};

This object has three properties:

  • name

  • age

  • isStudent


How to Create Objects

JavaScript gives you many ways.


1. Object Literal (Most Common)

const user = {
  username: "sam",
  score: 100
};

This is the simplest and most used method.


2. Using the Object Constructor

const user = new Object();
user.name = "Sam";
user.age = 22;

Not very common today.


3. Using a Function (Constructor Function)

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const p1 = new Person("Asha", 20);

Useful in older codebases.


class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const p1 = new Person("Asha", 20);

This is the modern OOP approach.


Accessing Object Properties

There are two ways:


1. Dot Notation

console.log(person.name);
console.log(person.age);

Dot notation is clean and easy.


2. Bracket Notation

console.log(person["name"]);

Use brackets when:

  • the key has spaces

  • the key has special characters

  • the key is dynamic

Example:

const key = "first-name";
const user = { "first-name": "Karan" };

console.log(user[key]); // Karan

Adding and Updating Properties

Add new property

person.city = "Mumbai";

Update existing property

person.age = 26;

Deleting Properties

Use the delete keyword:

delete person.isStudent;

Methods in Objects

A method is a function stored in an object.

const car = {
  brand: "Toyota",
  start() {
    console.log("Car started");
  }
};

car.start();

Nested Objects

Objects can contain other objects.

const student = {
  name: "Ravi",
  marks: {
    maths: 90,
    science: 95
  }
};

console.log(student.marks.maths); // 90

Objects Inside Arrays

Very common in real apps.

const users = [
  { name: "Sam", age: 20 },
  { name: "Mira", age: 22 }
];

console.log(users[1].name); // Mira

Looping Through Objects

1. for…in

for (let key in person) {
  console.log(key, person[key]);
}

2. Object.keys()

Object.keys(person); // ["name", "age"]

3. Object.values()

Object.values(person); // ["John", 25]

4. Object.entries()

Object.entries(person);
// [["name", "John"], ["age", 25]]

Useful for converting objects to arrays.


Copying Objects

Objects are copied by reference, not by value.

This means:

const a = { x: 1 };
const b = a;

b.x = 5;

console.log(a.x); // 5

Both variables point to the same object.


To actually copy an object:

1. Using Spread Operator

const copy = { ...person };

2. Using Object.assign()

const copy = Object.assign({}, person);

Checking if a Property Exists

Using in

"name" in person; // true

Using hasOwnProperty()

person.hasOwnProperty("age"); // true

Comparing Objects

Objects are never equal unless they reference the same memory.

{} === {} // false

This is a key difference from primitive types.


Common Mistakes

❌ Using dot notation for invalid keys

user.first-name // error

Use brackets.


❌ Confusing reference copy with value copy

const x = { a: 1 };
const y = x; // not a new copy

❌ Forgetting that arrays and functions are objects

typeof [] // "object"
typeof function() {} // "function" (but still an object)

Best Practices

✔ Use object literals when possible
✔ Use dot notation for clean code
✔ Use brackets for dynamic or special keys
✔ Use const for objects to avoid reassigning
✔ Use spread syntax to copy objects
✔ Use classes when modeling complex structures


Summary

ConceptExampleNotes
Create object{ name: "A" }Most common
Access propertyobj.nameDot notation
Dynamic accessobj[key]Bracket notation
Add propertyobj.age = 30
Delete propertydelete obj.age
Loop keysfor...inIterates over keys
Copy object{ ...obj }Shallow copy
Nested objectsobj.inner.valueVery common

More from this blog

Javascript Beginner Articles

23 posts