Learn and master the top JavaScript Object interview questions along with detailed answers and code snippets

  1. What is an object in JavaScript?
    An object in JavaScript is a collection of key-value pairs where the keys are unique identifiers and the values can be of any data type.

    It allows you to store and access multiple values as properties.
  2. How do you create an object in JavaScript?
    There are multiple ways to create an object in JavaScript:

    a. Using object literal notation:
   const person = { name: 'John', age: 25 };

b. Using the Object constructor:

   const person = new Object();
   person.name = 'John';
   person.age = 25;

c. Using the Object.create() method:

   const person = Object.create(null);
   person.name = 'John';
   person.age = 25;
  1. How do you access the properties of an object in JavaScript?
    You can access the properties of an object using dot notation or bracket notation: a. Dot notation:
   const person = { name: 'John', age: 25 };
   console.log(person.name); // Output: John

b. Bracket notation:

   const person = { name: 'John', age: 25 };
   console.log(person['name']); // Output: John
  1. How do you add or update the properties of an object in JavaScript?
    You can add or update the properties of an object using the assignment:
   const person = { name: 'John', age: 25 };
   person.name = 'Alice'; // Update property
   person.gender = 'Female'; // Add new property
  1. How do you delete properties of an object in JavaScript?
    You can delete the properties of an object using the delete keyword:
   const person = { name: 'John', age: 25 };
   delete person.age; // Delete property
  1. How do you iterate over the properties of an object in JavaScript?
    You can iterate over the properties of an object using for...in loop:
   const person = { name: 'John', age: 25 };
   for (let key in person) {
     console.log(key, person[key]);
   }
  1. How do you check if a property exists in an object in JavaScript?
    You can check if a property exists in an object using the hasOwnProperty() method or the in operator:
   const person = { name: 'John', age: 25 };
   console.log(person.hasOwnProperty('name')); // Output: true
   console.log('name' in person); // Output: true
  1. How do you get the number of properties in an object in JavaScript?
    You can get the number of properties in an object using the Object.keys() method:
   const person = { name: 'John', age: 25 };
   const numProperties = Object.keys(person).length;
   console.log(numProperties); // Output: 2
  1. How do you clone an object in JavaScript?
    There are multiple ways to clone an object in JavaScript: a. Using the spread operator:
   const original = { name: 'John', age: 25 };
   const clone = { ...original };

b. Using the Object.assign() method:

   const original = { name: 'John', age: 25 };
   const clone = Object.assign

({}, original);

c. Using JSON serialization:

   const original = { name: 'John', age: 25 };
   const clone = JSON.parse(JSON.stringify(original));
  1. What is the difference between a deep copy and a shallow copy of an object?
    A shallow copy of an object creates a new object with the same properties and references to the same values as the original object.

    However, if the values are objects, they will still be references to the same objects.
See also  Roles and Responsibilities of Test Analyst

A deep copy of an object creates a new object with the same properties and copies the values recursively.

This means that if the values are objects, new objects will be created with copies of their properties.

  1. How do you check if two objects are equal in JavaScript?
    In JavaScript, objects are compared by reference, not by value.

    To check if two objects are equal in terms of their properties and values, you can convert them to JSON strings and compare the strings:
   const obj1 = { name: 'John', age: 25 };
   const obj2 = { name: 'John', age: 25 };
   const obj1String = JSON.stringify(obj1);
   const obj2String = JSON.stringify(obj2);
   const isEqual = obj1String === obj2String;
   console.log(isEqual); // Output: true
  1. How do you merge two objects in JavaScript?
    You can merge two objects in JavaScript using the spread operator or the Object.assign() method:

    a. Using the spread operator:
   const obj1 = { name: 'John' };
   const obj2 = { age: 25 };
   const merged = { ...obj1, ...obj2 };

b. Using the Object.assign() method:

   const obj1 = { name: 'John' };
   const obj2 = { age: 25 };
   const merged = Object.assign({}, obj1, obj2);
  1. How do you freeze an object in JavaScript?
    You can freeze an object in JavaScript using the Object.freeze() method.

    Once an object is frozen, you cannot add, update, or delete properties from it:
   const obj = { name: 'John', age: 25 };
   Object.freeze(obj);
  1. What are getters and setters in JavaScript objects?
    Getters and setters are special methods in JavaScript objects that allow you to define the behavior of accessing and modifying properties.
   const person = {
     firstName: 'John',
     lastName: 'Doe',
     get fullName() {
       return `${this.firstName} ${this.lastName}`;
     },
     set fullName(value) {
       const [firstName, lastName] = value.split(' ');
       this.firstName = firstName;
       this.lastName = lastName;
     }
   };

   console.log(person.fullName); // Output: John Doe
   person.fullName = 'Alice Smith';
   console.log(person.firstName); // Output: Alice
   console.log(person.lastName); // Output: Smith
  1. What is object destructuring in JavaScript?
    Object destructuring is a feature in JavaScript that allows you to extract properties from an object and assign them to variables in a concise way:
   const person = { name: 'John', age: 25 };
   const { name, age } = person;
   console.log(name); // Output: John
   console.log(age); // Output: 25
  1. How do you prevent the modification of object properties in JavaScript?
    You can prevent the modification of object properties in JavaScript using the Object.defineProperty() method with the writable property descriptor set to false:
   const obj = { name: 'John', age: 25 };
   Object.defineProperty(obj, 'name', { writable: false });
   obj.name = 'Alice'; // This will not modify the value
  1. How do you create private variables in JavaScript objects?
    In JavaScript, you can create private variables in objects using closures:
   const person = (function() {
     let name = 'John';

     return {
       getName: function() {
         return name;
       }
     };
   })();

   console.log(person.getName()); // Output: John
   console.log(person.name); // Output: undefined
  1. How do you check if a variable is an object in JavaScript?
    You can check if a variable is an object in JavaScript using the typeof operator and comparing it to 'object':
   const obj = { name: 'John' };
   const variable = 10;
   console.log(typeof obj === 'object'); // Output: true
   console.log(typeof variable === 'object'); // Output: false
  1. How do you convert an object to an array in JavaScript?
    You can convert an object to an array in JavaScript using the Object.entries() method:
   const obj = { name: 'John', age: 25 };
   const arr = Object.entries(obj);
   console.log(arr); // Output: [['name', 'John'], ['age', 25]]
  1. How do you check if an object is empty in JavaScript?
    You can check if an object is empty in JavaScript by comparing the number of properties to 0:
   const obj = {};
   const isEmpty = Object.keys(obj).length === 0;
   console.log(isEmpty); // Output: true
  1. How do you create an object with a dynamic key in JavaScript?
    You can create an object with a dynamic key in JavaScript by using square brackets [] to define the key:
   const key = 'name';
   const value = 'John';
   const obj = { [key]: value };
   console.log(obj); // Output: { name: 'John' }
  1. How do you compare two objects in JavaScript?
    In JavaScript, objects are compared by reference, not by value.

    If you want to compare two objects by value, you can convert them to JSON strings and compare the strings:
   const obj1 = { name: 'John', age: 25 };
   const obj2 = { name: 'John', age: 25 };
   const obj1String = JSON.stringify(obj1);
   const obj2String = JSON.stringify(obj2);
   const isEqual = obj1String === obj2String;
   console.log(isEqual); // Output: true
  1. How do you iterate over the values of an object in JavaScript?
    You can iterate over the values of an object using Object.values() method:
   const obj = { name: 'John', age: 25 };
   const values = Object.values(obj);
   console.log(values); // Output: ['John', 25]
  1. How do you iterate over the keys and values of an object in JavaScript?
    You can iterate over the keys and values of an object using Object.entries() method:
   const obj = { name: 'John', age: 25 };
   for (let [key, value] of Object.entries(obj)) {
     console.log(key, value);
   }
  1. How do you check if an object has a specific key in JavaScript?
    You can check if an object has a specific key using the hasOwnProperty() method or the in operator:
   const obj = { name: 'John', age: 25 };
   console.log(obj.hasOwnProperty('name')); // Output: true
   console.log('name' in obj); // Output: true
  1. How do you get the keys of an object in JavaScript?
    You can get the keys of an object using the Object.keys() method:
   const obj = { name: 'John', age: 25 };
   const keys = Object.keys(obj);
   console.log(keys); // Output: ['name', 'age']
  1. How do you get the values of an object in JavaScript?
    You can get the values of an object using the Object.values() method:
   const obj = { name: 'John', age: 25 };
   const values = Object.values(obj);
   console.log(values); // Output: ['John', 25]
  1. How do you remove a property from an object in JavaScript?
    You can remove a property from an object using the delete keyword:
   const obj = { name: 'John', age: 25 };
   delete obj.age;
  1. How do you convert an object to a JSON string in JavaScript?
    You can convert an object to a JSON string using the JSON.stringify() method:
   const obj = { name: 'John', age: 25 };
   const jsonString = JSON.stringify(obj);
   console.log(jsonString); // Output: '{"name":"John","age":25}'
  1. How do you convert a JSON string to an object in JavaScript?
    You can convert a JSON string to an object using the JSON.parse() method:
   const jsonString = '{"name":"John","age":25}';
   const obj = JSON.parse(jsonString);
   console.log(obj); // Output: { name: 'John', age: 25 }