JavaScript Objects Practice Questions with Solutions

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

  1. student is an object.
  2. Objects are created using curly brackets {}.
  3. name, age, and course are called properties.
  4. Each property has a key and a value.
  5. 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

  1. The object contains three properties.
  2. student.name accesses the name property.
  3. JavaScript finds the value associated with name.
  4. 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

  1. The object contains name and price.
  2. Bracket notation uses square brackets.
  3. "price" is written inside quotes.
  4. product["price"] accesses the price value.
  5. 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

  1. The object initially stores age: 17.
  2. student.age accesses the age property.
  3. = 18 assigns a new value.
  4. The old value 17 is replaced.
  5. 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

  1. The object initially has name and age.
  2. student.city does not exist yet.
  3. Assigning "Delhi" creates the new property.
  4. 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

  1. The object initially contains three properties.
  2. The delete operator removes a property.
  3. delete user.age removes the age property.
  4. 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

  1. The object contains a property named course.
  2. The in operator checks whether a property exists in an object.
  3. "course" in student returns true.
  4. 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

  1. student is an object.
  2. greet is a function stored inside the object.
  3. A function inside an object is called a method.
  4. this.name refers to the name property of the current object.
  5. student.greet() calls the method.
  6. 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

  1. The product price is 50.
  2. The quantity is 4.
  3. product.price gets the price.
  4. product.quantity gets the quantity.
  5. The values are multiplied.
  6. 50 × 4 gives 200.
  7. 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

  1. student contains three properties.
  2. for...in loops through the object’s property keys.
  3. key contains the current property name.
  4. student[key] gets the value of that property.
  5. The loop repeats for every property.
  6. 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 delete operator removes an object property.
  • The in operator checks whether a property exists.
  • Functions stored inside objects are called methods.
  • this can refer to the current object inside an object method.
  • for...in can 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.

Scroll to Top