Arrays are useful for storing a list of values. But what if we want to store information about one thing, such as a student, car, or mobile phone? This is where JavaScript Objects are useful. An object stores related information together using key-value pairs.
JavaScript Objects
What is an Object?
An object is a collection of related data and actions. For example, a student can have a name, age, and course.
let student = {
name: "Rahul",
age: 14,
course: "JavaScript"
};
console.log(student);
Output:
{name: "Rahul", age: 14, course: "JavaScript"}
Here:
name,age, andcourseare called properties."Rahul",14, and"JavaScript"are their values.
Creating an Object
We create an object using curly brackets {}.
let car = {
brand: "Toyota",
model: "Camry",
year: 2025
};
Each property is written as:
key: value
Accessing Object Properties
We can access an object’s property using the dot . operator.
let student = {
name: "Aman",
age: 14
};
console.log(student.name);
console.log(student.age);
Output:
Aman
14
We can also use square brackets:
console.log(student["name"]);
Output:
Aman
Changing Object Properties
Object properties can be changed after creating the object.
let student = {
name: "Aman",
age: 14
};
student.age = 15;
console.log(student.age);
Output:
15
Adding and Deleting Properties
We can add a new property using the dot operator.
let student = {
name: "Aman",
age: 14
};
student.city = "Delhi";
console.log(student.city);
Output:
Delhi
We can delete a property using the delete keyword.
delete student.age;
console.log(student);
Nested Objects
An object can contain another object. This is called a nested object.
let student = {
name: "Aman",
address: {
city: "Delhi",
country: "India"
}
};
console.log(student.address.city);
Output:
Delhi
JavaScript Methods and this
What is an Object Method?
A method is a function stored inside an object.
let student = {
name: "Aman",
greet: function() {
console.log("Hello!");
}
};
student.greet();
Output:
Hello!
Here, greet is an object method.
The this Keyword
The this keyword refers to the current object. It is commonly used inside object methods.
let student = {
name: "Aman",
greet: function() {
console.log("Hello " + this.name);
}
};
student.greet();
Output:
Hello Aman
Here, this.name means the name property of the current student object.
JavaScript Constructor
What is a Constructor Function?
A constructor function helps us create multiple objects with the same structure. We use the new keyword to create objects from a constructor.
function Student(name, age) {
this.name = name;
this.age = age;
}
let student1 = new Student("Aman", 14);
let student2 = new Student("Riya", 15);
console.log(student1.name);
console.log(student2.name);
Output:
Aman
Riya
The constructor lets us create many student objects without writing the same structure again and again.
JavaScript Getter and Setter
Getters and setters are used to read and change object properties in a controlled way.
Getter
A getter uses the get keyword to return a value.
let student = {
firstName: "Aman",
lastName: "Kumar",
get fullName() {
return this.firstName + " " + this.lastName;
}
};
console.log(student.fullName);
Output:
Aman Kumar
Notice that we use student.fullName like a property, not like a function.
Setter
A setter uses the set keyword to change a value.
let student = {
name: "Aman",
set studentName(newName) {
this.name = newName;
}
};
student.studentName = "Riya";
console.log(student.name);
Output:
Riya
The setter runs when we assign a new value.
JavaScript Prototype
What is a Prototype?
Every JavaScript object has a prototype. A prototype allows objects to share properties and methods. For example, we can add a method to a constructor’s prototype.
function Student(name) {
this.name = name;
}
Student.prototype.greet = function() {
console.log("Hello " + this.name);
};
let student1 = new Student("Aman");
let student2 = new Student("Riya");
student1.greet();
student2.greet();
Output:
Hello Aman
Hello Riya
The greet() method is added to the prototype, so objects created from Student can use it.
Why use Prototypes?
Prototypes are useful when we want many objects to share the same method instead of creating a separate copy of that method for every object.
Quick Recap
- An object stores related information using key-value pairs.
- Object properties store data.
- Object methods are functions inside objects.
thisrefers to the current object.- A constructor function helps create multiple similar objects.
- Getters read values and setters change values.
- A prototype allows objects to share properties and methods.
Frequently Asked Questions (FAQs)
Q1. What is a JavaScript Object?
A JavaScript Object stores related data and actions together using key-value pairs, such as a student’s name, age, and course.
Q2. How do you create an Object in JavaScript?
You can create a JavaScript Object using curly brackets {} and define its properties as key-value pairs.
Q3. What are JavaScript Object Methods?
JavaScript Object Methods are functions stored inside an object that can perform actions using the object’s data.
Q4. What is a JavaScript Constructor Function?
A constructor function is used to create multiple objects with the same structure using the new keyword.
Q5. What is a JavaScript Prototype?
A prototype allows JavaScript objects to share properties and methods, which is useful when working with multiple similar objects.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
