Introduction
In JavaScript, an object is a complex data structure that allows you to store collections of key-value pairs. Objects are one of the core components of JavaScript and are used to represent real-world entities, such as a user, a car, or any other item that has properties and behaviors.
OR
An object is used to store multiple values. An object has a key and a value pair.
What is a JavaScript Object?
A JavaScript Object is a collection of key-value pairs where
- A key is called a property name.
- A value can be any data type, such as a string, number, boolean, array, function, or even another object.
Object Syntax
The basic syntax of a JavaScript object is:
let objectName = {
property1: value1,
property2: value2,
property3: value3
};
Example of Object:
let car = {
brand: "Tata",
model: "Nexon",
year: 2024
};
Creating Objects in JavaScript
There are multiple ways to create objects in JavaScript.
1. Object Literal
This is the most common and easiest method.
Example:
let person = {
name: "John",
age: 35
};
2. Using new Object()
Create an object through a new keyword.
let person = new Object();
person.name = "John";
person.age = 35;
3. Using a Constructor Function
function Person(name, age) {
this.name = name;
this.age = age;
}
let user = new Person("John", 35);
4. Using ES6 Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let user = new Person("John", 35);
Accessing Object Properties
JavaScript provides two ways to access object properties.
1. Dot Notation
let person = {
name: "John",
age: 35
};
console.log(person.name);
Output:
2. Bracket Notation
Bracket notation is useful when property names contain spaces or special characters.
let person = {
name: "John",
age: 35
};
console.log(person["age"]);
Output:
Adding Properties to the Object
let person = {
name: "John"
};
person.age = 35;
console.log(person);
Output:
Updating Properties to Object
Existing properties can be modified easily.
let person = {
name: "John",
age: 35
};
person.age = 40;
console.log(person.age);
Output:
Deleting Properties from the Object
The delete keyword removes a property from an object.
let person = {
name: "John",
age: 35
};
delete person.age;
console.log(person);
Output:
What are Nested Objects?
Objects that contain other objects are called nested objects.
let employee = {
name: "John",
address: {
city: "New Delhi",
country: "India"
}
};
console.log(employee.address.city);
Output:
What is Object.keys()
It is used to return all property names.
let person = {
name: "John",
age: 35
};
console.log(Object.keys(person));
Output:
What is Object.values()
It is used to return all values.
let person = {
name: "John",
age: 35
};
console.log(Object.keys(person));
Output:
What is Object.entries()
It is used to return key-value pairs.
let person = {
name: "John",
age: 35
};
console.log(Object.entries(person));
Output:
Real-life Example:
Imagine you are developing an online shopping website.
let product = {
id: 101,
name: "Laptop",
price: 65000,
brand: "HP",
inStock: true
};
console.log(product.name);
console.log(product.price);
Output:
65000
Common Mistakes
1. Using Dot Notation with Spaces
Wrong:
let user = {
"full name": "John"
};
console.log(user.full name);
Correct:
let user = {
"full name": "John"
};
console.log(user["full name"]);
2. Forgetting Quotes Around Special Property Names
Wrong:
let person = {
full name: "John"
};
Correct:
let person = {
"full name": "John"
};
3. Using Assignment Instead of Comparison
Wrong:
if(person.age = 18) {
console.log("Adult");
}
Correct:
if(person.age === 18) {
console.log("Adult");
}
4. Accessing Non-Existing Properties
let person = {
name: "John"
};
console.log(person.age);
Output:
Note: Always verify whether a property exists.
let person = {
name: "John"
};
if(person.age !== undefined) {
console.log(person.age);
}
5. Forgetting Commas Between Properties
Wrong:
let person = {
name: "John"
age: 35
};
Correct:
let person = {
name: "John",
age: 35
};
Javascript Object – Interview Questions
Q 1: What is an object?
Q 2: How to create an object?
Q 3: How to access properties?
Q 4: Can objects have methods?
Q 5: Are objects mutable?
Javascript Object – Objective Questions (MCQs)
Q1. JavaScript objects store data in ______ pairs.
Q2. Which syntax creates an object?
Q3. How do you access object property name?
Q4. Which loop is commonly used to iterate object properties?
Q5. Objects in JavaScript are ______.
Conclusion
JavaScript Objects are one of the most powerful and essential features of the language. They allow developers to store related data and functionality in a structured way. Objects can contain properties, methods, arrays, and even other objects, making them highly flexible and useful for building real-world applications.