J

JavaScript Handbook

Clean • Professional

JavaScript Objects — Top Interview Questions & Answers

5 minute

JavaScript Objects — Interview Questions & Answers

Ques: What is an Object in JavaScript?

Ans: An object is a collection of key-value pairs where values can be data (properties) or functions (methods).

const person = {
  name: "Aarav",
  age: 25,
  greet() {
    console.log("Hello!");
  }
};

Ques: How can you create objects in JavaScript?

Ans: There are five main ways:

  1. Object Literal

    const obj = { name: "John" };
    
  2. Using new Object()

    const obj = new Object();
    obj.name = "John";
    
  3. Constructor Function

    function Person(name) { this.name = name; }
    const p1 = new Person("John");
    
  4. ES6 Class

    class Car { constructor(model) { this.model = model; } }
    
  5. Object.create()

    const base = { greet() { console.log("Hi"); } };
    const obj = Object.create(base);
    

Ques: What are Object Properties?

Ans: They are key-value pairs stored inside an object.

const user = { name: "Riya", age: 21 };
console.log(user.name); // Access
user.city = "Delhi"; // Add new

Ques: How can you access object properties?

  1. Dot notation: user.name
  2. Bracket notation: user["name"]

Ques: How can you delete or modify a property in an object?

const user = { name: "Alex", age: 30 };
delete user.age;   // delete
user.name = "Max"; // modify

Ques: How to check if a property exists in an object?

"name" in user; // true
user.hasOwnProperty("name"); // true

Ques: What are Object Methods?

Ans: Functions defined inside objects.

const user = {
  name: "Mira",
  greet() {
    console.log(`Hi ${this.name}`);
  }
};

Ques: What does this refer to inside an object method?

Ans: It refers to the object itself that owns the method.

user.greet(); // this → user

Ques: What are Computed Property Names?

Ans: Allows dynamic property names using expressions.

const prop = "age";
const user = { [prop]: 25 };

Ques: How can you loop through object properties?

for (let key in user) {
  console.log(key, user[key]);
}

Ques: What are Object.keys(), Object.values(), and Object.entries()?

  • Object.keys(obj) → returns property names
  • Object.values(obj) → returns values
  • Object.entries(obj) → returns [key, value] pairs

Ques: What is Object Destructuring?

Ans: Extracting properties into variables.

const person = { name: "Sara", age: 22 };
const { name, age } = person;

Ques: How can you copy objects in JavaScript?

  1. Shallow copy

    const copy = { ...obj };
    const copy2 = Object.assign({}, obj);
    
  2. Deep copy

    const deepCopy = structuredClone(obj);
    

Ques: What is the difference between Shallow and Deep Copy?

Copy TypeDescription
ShallowOnly top-level properties are copied
DeepNested objects are copied too

Ques: What are Object Prototypes?

Ans: Every JavaScript object has a prototype — another object it inherits properties from.

const arr = [];
console.log(Object.getPrototypeOf(arr)); // Array.prototype

Ques: What is the Prototype Chain?

Ans: A chain through which property lookups happen when they’re not found on the current object.

Ques: What is the difference between __proto__ and prototype?

TermUsed WithMeaning
__proto__InstancePoints to prototype of constructor
prototypeFunctionUsed to define inherited properties

Ques: What is Object Inheritance in JavaScript?

Ans: Objects can inherit properties and methods from another object using the prototype chain or Object.create().

Ques: How to create inheritance using Object.create()?

const parent = { greet() { console.log("Hi"); } };
const child = Object.create(parent);
child.greet(); // Hi

Ques: What are Getters and Setters in objects?

Ans: Used to define computed properties or controlled access.

const user = {
  first: "Amit",
  last: "Kumar",
  get fullName() { return this.first + " " + this.last; },
  set fullName(name) { [this.first, this.last] = name.split(" "); }
};

Ques: What is Object.freeze()?

Ans: Prevents adding, deleting, or modifying properties.

const obj = Object.freeze({ x: 10 });
obj.x = 20; //  ignored

Ques: What is Object.seal()?

Ans: Prevents adding/removing properties, but allows modifying existing ones.

const obj = Object.seal({ x: 10 });
obj.x = 20; //  works

Ques: What is Object.preventExtensions()?

Ans: Prevents adding new properties, but allows modifying or deleting existing ones.

Ques: What is the Object.assign() method used for?

Ans: Copies properties from one or more source objects to a target object.

Object.assign(target, source);

Ques: What is Object Reference in JavaScript?

Ans: Objects are assigned by reference, not by value.

const a = { x: 1 };
const b = a;
b.x = 2;
console.log(a.x); // 2

Ques: How to clone an object without reference issues?

Ans: Use the spread operator or structuredClone for deep copy.

Ques: What is JSON.stringify() and JSON.parse()?

Ans: Used to convert between objects and JSON strings.

const json = JSON.stringify(obj);
const parsed = JSON.parse(json);

Ques: What are Constructor Functions?

Ans: Functions used to create multiple similar objects.

function Car(model) {
  this.model = model;
}
const c1 = new Car("BMW");

Ques: What is the new keyword used for?

Ans: Creates a new instance of an object with its prototype linked to the constructor’s prototype.

Ques: How does this behave inside an object method?

Ans: Refers to the object that invoked the method.

Ques: What happens when you use this outside any object?

  • In non-strict mode → global object (window)
  • In strict modeundefined

Ques: What is Object.defineProperty()?

Ans: Used to define or modify a property’s attributes (e.g., writable, enumerable).

Object.defineProperty(obj, "x", { value: 42, writable: false });

Ques: What is Object.entries() used for?

Ans: Returns an array of [key, value] pairs.

Object.entries({a:1,b:2}); // [["a",1],["b",2]]

Ques: What is Object.fromEntries()?

Ans: Converts key-value pairs back to an object.

Object.fromEntries([["a",1],["b",2]]);

Ques: How can you merge multiple objects?

const merged = { ...obj1, ...obj2 };

Ques: How can you convert an object to an array?

Object.entries(obj);

Ques: How can you check if an object is empty?

Object.keys(obj).length === 0;

Ques: What are the advantages of using objects in JavaScript?

  • Data organization
  • Reusability with prototypes
  • Easy modeling of real-world entities
  • Supports inheritance and encapsulation

Article 0 of 0