Edit Template
Edit Template

JavaScript Objects

In JavaScript, objects are used to represent real-world entities like a student, car, or book. They are a core part of Object-Oriented Programming (OOP) and allow you to store and manage related data and behaviors together.

What Is an Object?

An object is a collection of key-value pairs. Each key is called a property, and when a property stores a function, it’s called a method.

Objects are declared using curly braces {}, known as object literal syntax.

				
					let person = {
  name: "John",
  age: 30,
  greet: function () {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  }
};

				
			

Accessing Object Properties

You can access properties in two ways:

				
					console.log(person.name);     // Dot notation → "John"
console.log(person["age"]);   // Bracket notation → 30

				
			

Both notations are valid. Bracket notation is useful when the key is stored in a variable or has spaces.

Modifying Objects

You can easily add, update, or delete properties from an object:

				
					person.address = "New York";           // Add
person["phone"] = "123-456-7890";      // Add
person.name = "Mike";                  // Update
delete person.age;                     // Delete

				
			

Object Methods

Objects can contain methods — functions defined inside the object:

				
					console.log(person.greet());
// Output: "Hello, my name is Mike and I am undefined years old."

				
			

(Notice that we deleted age earlier.)

Creating Objects Using Constructor Functions

You can also create objects using a constructor function — a function designed to initialize new objects using the new keyword:

				
					function Person(name, age) {
  this.name = name;
  this.age = age;
  this.greet = function () {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  };
}

let person1 = new Person("Ali", 28);
let person2 = new Person("Areeba", 24);

console.log(person1.greet());

				
			

This is the foundation of classical OOP in JavaScript before ES6 classes.

Built-in Object Methods

JavaScript provides useful built-in methods for working with objects:

  • hasOwnProperty(prop) – checks if the object has a specific property.

  • toString() – returns a string representation of the object.

  • valueOf() – returns the primitive value of the object.

Example:

				
					console.log(person1.hasOwnProperty("name")); // true

				
			

Creating an Empty Object

You can also use the built-in Object() constructor to create an empty object:

				
					let emptyObj = new Object();
emptyObj.title = "Untitled";

				
			

Summary

  • Objects store data using key-value pairs.

  • You can use either object literals {} or constructor functions to create objects.

  • Objects can store both values and functions (methods).

  • Objects are the building blocks of JavaScript’s OOP model.

Understanding how to work with objects is essential for writing clean, organized, and reusable JavaScript code.

Scroll to Top