Introductions
JavaScript objects are used to store related information in key-value pairs. They are extremely useful for representing real-world data such as students, employees, products, cars, and users. In this chapter, you will practice creating objects, accessing properties, changing values, adding and deleting properties, and working with object methods through simple examples. JavaScript Objects practice questions with solutions help to build concepts.
Question 1: Create and Display an Object
Problem
Create a student object containing a student’s name, age, and course. Display the complete object.
Solution
let student = {
name: "Rahul",
age: 18,JavaScript objects examples
course: "JavaScript"
};
console.log(student);
Output
{
name: 'Rahul',
age: 18,
course: 'JavaScript'
}
Step-by-step Explanation
studentis an object.- Objects are created using curly brackets
{}. name,age, andcourseare called properties.- Each property has a key and a value.
console.log()displays the complete object.
Question 2: Access an Object Property
Problem
Create a student object and display only the student’s name using dot notation.
Solution
let student = {
name: "Aman",
age: 17,
course: "Web Development"
};
console.log(student.name);
Output
Aman
Step-by-step Explanation
- The object contains three properties.
student.nameaccesses thenameproperty.- JavaScript finds the value associated with
name. - The value
"Aman"is displayed.
Question 3: Access a Property Using Bracket Notation
Problem
Create a product object and display its price using bracket notation.
Solution
let product = {
name: "Laptop",
price: 50000
};
console.log(product["price"]);
Output
50000
Step-by-step Explanation
- The object contains
nameandprice. - Bracket notation uses square brackets.
"price"is written inside quotes.product["price"]accesses the price value.- The output is
50000.
Both of these approaches work:
product.price;
product["price"];
Question 4: Change an Object Property
Problem
A student’s age is currently 17. Change it to 18.
Solution
let student = {
name: "Priya",
age: 17
};
student.age = 18;
console.log(student);
Output
{
name: 'Priya',
age: 18
}
Step-by-step Explanation
- The object initially stores
age: 17. student.ageaccesses theageproperty.= 18assigns a new value.- The old value
17is replaced. - The student’s age is now
18.
Question 5: Add a New Property
Problem
Create a student object containing a name and age. Add a new city property.
Solution
let student = {
name: "Neha",
age: 19
};
student.city = "Delhi";
console.log(student);
Output
{
name: 'Neha',
age: 19,
city: 'Delhi'
}
Step-by-step Explanation
- The object initially has
nameandage. student.citydoes not exist yet.- Assigning
"Delhi"creates the new property. - The object now contains three properties.
Question 6: Delete an Object Property
Problem
Create a user object containing a name, email, and age. Remove the age property.
Solution
let user = {
name: "Rahul",
email: "rahul@example.com",
age: 20
};
delete user.age;
console.log(user);
Output
{
name: 'Rahul',
email: 'rahul@example.com'
}
Step-by-step Explanation
- The object initially contains three properties.
- The
deleteoperator removes a property. delete user.ageremoves theageproperty.- The remaining properties stay unchanged.
Question 7: Check Whether an Object Has a Property
Problem
Create a student object and check whether it contains a course property.
Solution
let student = {
name: "Aman",
age: 18,
course: "JavaScript"
};
console.log("course" in student);
Output
true
Step-by-step Explanation
- The object contains a property named
course. - The
inoperator checks whether a property exists in an object. "course" in studentreturnstrue.- If the property did not exist, the result would be
false.
Question 8: Create an Object with a Method
Problem
Create a student object with a greet() method that displays "Hello, I am Rahul".
Solution
let student = {
name: "Rahul",
greet: function() {
console.log("Hello, I am " + this.name);
}
};
student.greet();
Output
Hello, I am Rahul
Step-by-step Explanation
studentis an object.greetis a function stored inside the object.- A function inside an object is called a method.
this.namerefers to thenameproperty of the current object.student.greet()calls the method.- The message is displayed.
Question 9: Calculate a Product’s Total Price
Problem
Create a product object containing price and quantity. Calculate and display the total price.
Solution
let product = {
name: "Notebook",
price: 50,
quantity: 4
};
let total = product.price * product.quantity;
console.log(total);
Output
200
Step-by-step Explanation
- The product price is
50. - The quantity is
4. product.pricegets the price.product.quantitygets the quantity.- The values are multiplied.
50 × 4gives200.- The total price is displayed.
Question 10: Loop Through Object Properties
Problem
Create a student object and use a for...in loop to display all property names and their values.
Solution
let student = {
name: "Riya",
age: 18,
course: "JavaScript"
};
for (let key in student) {
console.log(key + ": " + student[key]);
}
Output
name: Riya
age: 18
course: JavaScript
Step-by-step Explanation
studentcontains three properties.for...inloops through the object’s property keys.keycontains the current property name.student[key]gets the value of that property.- The loop repeats for every property.
- All keys and values are displayed.
For example:
key = "name"
student[key] = "Riya"
Then:
key = "age"
student[key] = 18
And finally:
key = "course"
student[key] = "JavaScript"
Key Takeaways
- Objects store related information using key-value pairs.
- Objects are created using
{}. - Object properties have a key and a value.
- Dot notation is commonly used to access properties.
- Bracket notation is useful when accessing properties dynamically.
- Existing property values can be changed.
- New properties can be added using assignment.
- The
deleteoperator removes an object property. - The
inoperator checks whether a property exists. - Functions stored inside objects are called methods.
thiscan refer to the current object inside an object method.for...incan be used to loop through object properties.
FAQs
1. What is an object in JavaScript?
An object is a collection of related data stored as key-value pairs.
let person = {
name: "Rahul",
age: 20
};
2. How do I access an object property?
You can use dot notation:
console.log(person.name);
Or bracket notation:
console.log(person["name"]);
3. What is the difference between dot notation and bracket notation?
Dot notation uses the property name directly:
person.name;
Bracket notation uses a string inside square brackets:
person["name"];
Bracket notation is especially useful when the property name is stored in a variable.
4. Can I add new properties to an object?
Yes. You can add a property by assigning a value to it.
let person = {
name: "Aman"
};
person.age = 18;
5. How can I change an object property?
Assign a new value to the property.
person.age = 19;
6. How can I remove an object property?
Use the delete operator.
delete person.age;
7. What is a method in a JavaScript object?
A method is a function stored as a property of an object.
let person = {
name: "Rahul",
sayHello: function() {
console.log("Hello");
}
};
person.sayHello();
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
