Introduction
Objects are one of the most important concepts in JavaScript. They allow you to store and organize data in key value pairs. Objects help represent real world entities such as a user, product, or order in a structured way.
Understanding Objects
An object is a collection of properties. Each property has a key also called a name and a value. Values can be numbers, strings, arrays, functions, or even other objects.
Example of an Object
let user = {
name: "Ali",
age: 22,
isStudent: true
};
In this example name age and isStudent are properties of the object.
Accessing Object Properties
You can access object values in two ways
Dot notation
console.log(user.name);
Bracket notation
console.log(user["age"]);
Both methods are useful depending on the situation.
Adding and Updating Properties
You can add new properties or update existing ones easily
user.city = "Lahore";
user.age = 23;
Removing Properties
To delete a property from an object use the delete keyword
delete user.isStudent;
Objects with Functions
Objects can also store functions. When a function is inside an object it is called a method.
let person = {
name: "Sara",
greet: function() {
console.log("Hello");
}
};
person.greet();
Why Objects Are Important
Objects help organize complex data
They make code more readable and manageable
They are widely used in APIs and web applications
They allow grouping related data and functionality
Conclusion
Understanding object basics is essential for mastering JavaScript. Objects are used in almost every application and form the foundation for advanced concepts like classes and frameworks.