JavaScript Objects
In JavaScript, objects are one of the most powerful and commonly used data structures. Objects allow you to store and manage collections of related data in a structured way. They are used extensively in both simple and complex JavaScript applications.
A JavaScript object is a collection of key-value pairs, where each key (also called a property) is a string, and the corresponding value can be any valid JavaScript data type, such as a number, string, array, function, or even another object.
Objects allow you to store related data together in a single unit, which makes them perfect for modeling complex structures, like a person’s details or an entire product catalog.
There are two primary ways to create objects in JavaScript:
new Object()
Syntax: This method is less commonly used but still valid.The simplest way to create an object is by using curly braces {}
with key-value pairs. Here’s an example:
const person = {
name: "John Doe",
age: 30,
job: "Software Developer"
};
console.log(person); // Output: { name: "John Doe", age: 30, job: "Software Developer" }
Explanation:
name
, age
, and job
are the keys (or properties) of the object."John Doe"
, 30
, and "Software Developer"
are the values corresponding to each key.new Object()
You can also create an object using the new Object()
syntax:
const person = new Object();
person.name = "John Doe";
person.age = 30;
person.job = "Software Developer";
console.log(person); // Output: { name: "John Doe", age: 30, job: "Software Developer" }
Explanation:
new Object()
syntax is used to create an empty object.You can access the values of an object using either dot notation or bracket notation.
Dot notation is the most common and easiest way to access properties in an object.
console.log(person.name); // Output: John Doe
console.log(person.age); // Output: 30
Explanation:
person.name
accesses the name
property of the person
object.Bracket notation is useful when you have property names that are not valid identifiers (e.g., spaces or special characters) or when the property name is stored in a variable.
console.log(person["job"]); // Output: Software Developer
const prop = "age";
console.log(person[prop]); // Output: 30
Explanation:
You can modify the values of existing properties or add new properties to an object at any time.
You can simply assign a new value to an existing property to modify it:
person.age = 31;
console.log(person.age); // Output: 31
Explanation:
age
property is updated from 30
to 31
using dot notation.You can add new properties to an object using dot notation or bracket notation:
person.email = "john.doe@example.com";
person["address"] = "123 Main St, Springfield, USA";
console.log(person);
Explanation:
email
and address
are added to the person
object.You can remove properties from an object using the delete
operator.
delete person.job;
console.log(person); // Output: { name: "John Doe", age: 31, email: "john.doe@example.com", address: "123 Main St, Springfield, USA" }
Explanation:
job
property is deleted from the person
object using the delete
operator.You may need to iterate over an object’s properties, especially when working with dynamic or complex data structures.
for...in
LoopThe for...in
loop allows you to iterate over the keys (properties) of an object.
for (let key in person) {
console.log(key + ": " + person[key]);
}
Output:
name: John Doe
age: 31
email: john.doe@example.com
address: 123 Main St, Springfield, USA
Explanation:
for...in
loop iterates over each key in the person
object.person[key]
is used to access the value corresponding to each key.JavaScript objects can contain other objects as values. These are called nested objects.
const person = {
name: "John Doe",
address: {
street: "123 Main St",
city: "Springfield",
country: "USA"
}
};
console.log(person.address.city); // Output: Springfield
Explanation:
address
property of person
is itself an object, which contains the properties street
, city
, and country
.person.address.city
).In JavaScript, objects can also have methods—functions that are stored as properties of an object.
const person = {
name: "John Doe",
age: 31,
greet: function() {
console.log("Hello, my name is " + this.name);
}
};
person.greet(); // Output: Hello, my name is John Doe
Explanation:
greet
method is a function stored as a property of the person
object.this
keyword refers to the current object (person
), allowing you to access its properties.Object destructuring is a concise way to extract multiple properties from an object into individual variables.
const person = {
name: "John Doe",
age: 31,
job: "Software Developer"
};
const { name, age } = person;
console.log(name); // Output: John Doe
console.log(age); // Output: 31
Explanation:
{ name, age }
extracts the name
and age
properties from the person
object into variables with the same names.