Learn and master the top JavaScript Object interview questions along with detailed answers and code snippets
- 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. - 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;
- 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
- 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
- How do you delete properties of an object in JavaScript?
You can delete the properties of an object using thedelete
keyword:
const person = { name: 'John', age: 25 };
delete person.age; // Delete property
- How do you iterate over the properties of an object in JavaScript?
You can iterate over the properties of an object usingfor...in
loop:
const person = { name: 'John', age: 25 };
for (let key in person) {
console.log(key, person[key]);
}
- How do you check if a property exists in an object in JavaScript?
You can check if a property exists in an object using thehasOwnProperty()
method or thein
operator:
const person = { name: 'John', age: 25 };
console.log(person.hasOwnProperty('name')); // Output: true
console.log('name' in person); // Output: true
- How do you get the number of properties in an object in JavaScript?
You can get the number of properties in an object using theObject.keys()
method:
const person = { name: 'John', age: 25 };
const numProperties = Object.keys(person).length;
console.log(numProperties); // Output: 2
- 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));
- 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.
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.
- 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
- 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);
- How do you freeze an object in JavaScript?
You can freeze an object in JavaScript using theObject.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);
- 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
- 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
- How do you prevent the modification of object properties in JavaScript?
You can prevent the modification of object properties in JavaScript using theObject.defineProperty()
method with thewritable
property descriptor set tofalse
:
const obj = { name: 'John', age: 25 };
Object.defineProperty(obj, 'name', { writable: false });
obj.name = 'Alice'; // This will not modify the value
- 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
- How do you check if a variable is an object in JavaScript?
You can check if a variable is an object in JavaScript using thetypeof
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
- How do you convert an object to an array in JavaScript?
You can convert an object to an array in JavaScript using theObject.entries()
method:
const obj = { name: 'John', age: 25 };
const arr = Object.entries(obj);
console.log(arr); // Output: [['name', 'John'], ['age', 25]]
- 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 to0
:
const obj = {};
const isEmpty = Object.keys(obj).length === 0;
console.log(isEmpty); // Output: true
- 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' }
- 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
- How do you iterate over the values of an object in JavaScript?
You can iterate over the values of an object usingObject.values()
method:
const obj = { name: 'John', age: 25 };
const values = Object.values(obj);
console.log(values); // Output: ['John', 25]
- 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 usingObject.entries()
method:
const obj = { name: 'John', age: 25 };
for (let [key, value] of Object.entries(obj)) {
console.log(key, value);
}
- How do you check if an object has a specific key in JavaScript?
You can check if an object has a specific key using thehasOwnProperty()
method or thein
operator:
const obj = { name: 'John', age: 25 };
console.log(obj.hasOwnProperty('name')); // Output: true
console.log('name' in obj); // Output: true
- How do you get the keys of an object in JavaScript?
You can get the keys of an object using theObject.keys()
method:
const obj = { name: 'John', age: 25 };
const keys = Object.keys(obj);
console.log(keys); // Output: ['name', 'age']
- How do you get the values of an object in JavaScript?
You can get the values of an object using theObject.values()
method:
const obj = { name: 'John', age: 25 };
const values = Object.values(obj);
console.log(values); // Output: ['John', 25]
- How do you remove a property from an object in JavaScript?
You can remove a property from an object using thedelete
keyword:
const obj = { name: 'John', age: 25 };
delete obj.age;
- How do you convert an object to a JSON string in JavaScript?
You can convert an object to a JSON string using theJSON.stringify()
method:
const obj = { name: 'John', age: 25 };
const jsonString = JSON.stringify(obj);
console.log(jsonString); // Output: '{"name":"John","age":25}'
- How do you convert a JSON string to an object in JavaScript?
You can convert a JSON string to an object using theJSON.parse()
method:
const jsonString = '{"name":"John","age":25}';
const obj = JSON.parse(jsonString);
console.log(obj); // Output: { name: 'John', age: 25 }