What is an object in JavaScript?
A collection of key-value pairs used to store related data and functions.
How do you create an object literal?
Use curly braces {}. Example: const person = { name: 'John', age: 30 };
How do you access an object property?
Use dot or bracket notation. Example: person.name or person['name'];
How do you add a new property to an object?
Assign a value to a new key. Example: person.job = 'Designer';
How do you remove a property from an object?
Use the delete keyword. Example: delete person.age;
How do you check if an object has a property?
Use the in operator or .hasOwnProperty(). Example: 'name' in person or person.hasOwnProperty('name');
How do you loop through object properties?
Use a for...in loop. Example: for (let key in person) { console.log(key, person[key]); }
How do you get all property names from an object?
Use Object.keys(obj). Example: Object.keys(person);
How do you get all property values from an object?
Use Object.values(obj). Example: Object.values(person);
How do you get both keys and values from an object?
Use Object.entries(obj). Example: Object.entries(person);
How do you merge multiple objects?
Use Object.assign(target, ...sources) or the spread operator {...obj1, ...obj2}.
How do you create a shallow copy of an object?
Use {...obj} or Object.assign({}, obj);
How do you freeze an object to make it immutable?
Use Object.freeze(obj);
How do you prevent adding or removing properties to an object?
Use Object.seal(obj);
How do you create a new object with a specific prototype?
Use Object.create(proto);
How do you get the prototype of an object?
Use Object.getPrototypeOf(obj);
How do you define methods inside an object?
Use function expressions or shorthand syntax. Example: const person = { greet() { console.log('Hi'); } };
What does this refer to inside an object method?
It refers to the object that the method belongs to.
How do you convert an object to a JSON string?
Use JSON.stringify(obj);
How do you parse a JSON string back to an object?
Use JSON.parse(jsonString);
What is destructuring in objects?
Extracting values from objects into variables. Example: const { name, age } = person;
How do you rename a property while destructuring?
Use a colon. Example: const { name: fullName } = person;
How do you provide a default value during destructuring?
Use =. Example: const { age = 25 } = person;
How do you check if two objects are equal?
Compare their keys and values manually or use JSON.stringify(obj1) === JSON.stringify(obj2);