Object Definition in JavaScript
In JavaScript, an object is a data structure that stores data as key–value pairs.
Each key (property name) is usually a string or symbol, and each value can be any data type—such as numbers, strings, arrays, functions, or other objects.
Objects are used to represent real-world entities (like a user, car, or product) by grouping related data and behavior together, making them a flexible way to organize and manage information in JavaScript.
Basic Syntax
Objects are commonly defined using object literals, which use curly braces {}
to contain key-value pairs separated by commas.
const person = {
name: "John",
age: 30,
profession: "Engineer"
};
Accessing Object Properties
You can access an object’s properties in two ways:
1. Dot Notation
Used when the property name is known and valid.
console.log(person.name); // "John"
2. Bracket Notation
Used when property names are dynamic or contain special characters.
console.log(person["age"]); // 30
const key = "profession";
console.log(person[key]); // "Engineer"
Adding, Modifying, and Deleting Properties
JavaScript objects are dynamic, meaning you can add, modify, or delete properties at runtime.
Add a Property
person.country = "India";
console.log(person);
// { name: "John", age: 30, profession: "Engineer", country: "India" }
Modify a Property
person.age = 31;
console.log(person.age); // 31
Delete a Property
delete person.profession;
console.log(person);
// { name: "John", age: 31, country: "India" }
Computed Property Names (ES6+)
Allows defining dynamic property keys using expressions.
const prop = "id";
const obj = { [prop]: 123 };
console.log(obj.id); // 123
Objects Can Contain Functions (Methods)
When a function is defined inside an object, it’s called a method.
Methods allow objects to perform actions or define behavior.
const car = {
brand: "Tesla",
start: function() {
console.log("Car started");
}
};
car.start(); // "Car started"
ES6 Shorthand Method Syntax
You can define methods more concisely:
const car = {
brand: "Tesla",
start() {
console.log("Car started");
}
};
Using this
Keyword
this
refers to the current object inside a method.
const car = {
brand: "Tesla",
start() {
console.log(`${this.brand} started`);
}
};
car.start(); // "Tesla started"
Key Features of JavaScript Objects
1. Dynamic
Objects are flexible — you can add, update, or remove properties at any time.
person.hobby = "Reading";
2. Nested
Objects can contain other objects, arrays, or complex structures.
const student = {
name: "Sara",
marks: {
math: 90,
science: 85
}
};
console.log(student.marks.math); // 90
3. Reference Type
Objects are stored and compared by reference, not by value.
const obj1 = { a: 1 };
const obj2 = obj1;
obj2.a = 2;
console.log(obj1.a); // 2
4. Foundation of JavaScript
Many JavaScript structures — such as arrays, functions, and dates — are special types of objects.
console.log(typeof []); // "object"
Example: Nested Object
Objects can represent hierarchical or structured data.
const student = {
name: "Sara",
marks: {
math: 90,
science: 85
},
details: {
address: {
city: "Mumbai",
country: "India"
}
}
};
console.log(student.marks.math); // 90