Javascript Interview Questions

JavaScript Interview Questions - Objects & Prototypes

35 interview questions on object creation, prototypes, inheritance, Object methods, getters/setters, property descriptors, and the class syntax.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

35 interview questions on object creation, prototypes, inheritance, Object methods, getters/setters, property descriptors, and the class syntax. This interview-focused guide covers essential javascript interview questions - objects & prototypes concepts for technical interviews.

JavaScript Interview Questions – Objects & Prototypes

Objects are the heart of JavaScript. These 35 questions cover object creation patterns, prototype-based inheritance, property descriptors, Object static methods, getters/setters, and the class syntax.


1. How many ways can you create objects in JavaScript?

// 1. Object literal
const obj = { name: "Alice" };

// 2. Object constructor
const obj = new Object();

// 3. Object.create()
const obj = Object.create(proto);

// 4. Constructor function
function Person(name) { this.name = name; }
const p = new Person("Alice");

// 5. ES6 Class
class User { constructor(name) { this.name = name; } }
const u = new User("Alice");

// 6. Factory function
const create = name => ({ name, greet() { return `Hi, ${name}`; } });

2. What is Object.create()?

Creates a new object with the specified prototype object and optional properties.

const person = { greet() { return `Hello, ${this.name}`; } };
const alice = Object.create(person);
alice.name = "Alice";
console.log(alice.greet()); // "Hello, Alice" — delegated to prototype

3. What are property descriptors?

Every object property has attributes: value, writable, enumerable, configurable.

const obj = {};
Object.defineProperty(obj, "secret", {
    value: "hidden",
    writable: false,
    enumerable: false,
    configurable: false
});

4. What is the difference between data and accessor descriptors?

  • Data descriptors: value, writable, enumerable, configurable
  • Accessor descriptors: get, set, enumerable, configurable

5. What are getters and setters?

Intercept property access and assignment.

const user = {
    firstName: "Alice", lastName: "Smith",
    get fullName() { return `${this.firstName} ${this.lastName}`; },
    set fullName(val) { [this.firstName, this.lastName] = val.split(" "); }
};

6. How does for...in differ from Object.keys()?

  • for...in — iterates all enumerable properties including inherited ones
  • Object.keys() — returns only own enumerable properties

7. What is hasOwnProperty()?

Checks if a property exists directly on the object (not inherited from prototype).

obj.hasOwnProperty("name"); // true (own)
obj.hasOwnProperty("toString"); // false (inherited)

8. What is Object.freeze() vs Object.seal()?

  • Object.freeze(): No add, delete, or modify (fully immutable)
  • Object.seal(): No add or delete, but existing props can be modified

[!NOTE] Both are shallow — nested objects can still be modified. Use Object.freeze(obj.nested) recursively for deep freeze.

9. What is Object.preventExtensions()?

Prevents new properties from being added. Existing ones can be modified and deleted.

10. What is the difference between freeze, seal, and preventExtensions?

  • preventExtensions: Can't add new properties
  • seal: Can't add or delete. Existing can be modified
  • freeze: Can't add, delete, or modify (fully immutable)

11. What does Object.assign() do?

Copies enumerable own properties from sources to target. Returns the target object. Shallow copy only.

const target = { a: 1 };
const source = { b: 2, c: 3 };
Object.assign(target, source); // target is now { a: 1, b: 2, c: 3 }

12. What is Object.is()?

Similar to === but correctly handles NaN and -0.

Object.is(NaN, NaN); // true (=== returns false)
Object.is(+0, -0);    // false (=== returns true)

13. What are Object.entries(), Object.values(), Object.keys()?

  • Object.keys(obj) — returns own enumerable property keys
  • Object.values(obj) — returns own enumerable property values
  • Object.entries(obj) — returns [[key, value], ...]

14. What is Object.fromEntries()?

Reverse of Object.entries() — converts key-value pairs array back to an object.

const obj = Object.fromEntries([["a", 1], ["b", 2]]);
// { a: 1, b: 2 }

15. What is prototype-based inheritance?

Objects inherit directly from other objects via the [[Prototype]] chain. No classes needed.

const animal = { eat() { return "eating"; } };
const dog = Object.create(animal);
dog.bark = () => "woof";
dog.eat(); // "eating" (inherited)
dog.bark(); // "woof" (own)

16. What is the prototype chain?

obj -> Constructor.prototype -> Object.prototype -> null

const arr = [1, 2, 3];
arr.toString(); // Found on Object.prototype via chain

17. What is __proto__ vs prototype?

  • prototype — property on constructor functions. Used as prototype for instances.
  • __proto__ — getter/setter on all objects to access internal [[Prototype]]. Deprecated — use Object.getPrototypeOf().

18. What is Object.getPrototypeOf()?

Returns the prototype (internal [[Prototype]]) of an object.

19. What is Object.setPrototypeOf()?

Sets the prototype of an object. Slow operation — prefer Object.create().

[!CAUTION] Changing prototype at runtime is very slow. Set it once at creation time with Object.create().

20. How do ES6 classes work under the hood?

class syntax is syntactic sugar over prototype-based inheritance.

class Animal {
    constructor(name) { this.name = name; }
    speak() { return `${this.name} makes a sound`; }
}
// Equivalent to:
function Animal(name) { this.name = name; }
Animal.prototype.speak = function() { return `${this.name} makes a sound`; };

21. What is super in classes?

Calls parent class constructor or methods. Must call super() before using this in constructor.

22. What is extends?

Creates a class as a child of another class, inheriting methods and properties.

23. What are public class fields?

Properties declared directly in the class body without this.

class Counter {
    count = 0; // Public field
    increment() { this.count++; }
}

24. What are private class fields (#)?

Truly private fields — not accessible outside the class, even in subclasses.

class Counter {
    #count = 0;
    increment() { this.#count++; }
    get value() { return this.#count; }
}

25. What are static methods and properties?

Called on the class itself, not on instances.

class Config {
    static API_URL = "https://api.example.com";
    static getBaseURL() { return this.API_URL; }
}

26. What is a computed property name?

Allows using an expression as a property name inside object literals.

const key = "status";
const obj = { [key]: "active", [`get${key}`]() { return this[key]; } };

27. What is property shorthand?

When variable name matches property name.

const name = "Alice", age = 25;
const obj = { name, age }; // Same as { name: name, age: age }

28. What is method shorthand?

Short syntax for defining methods in object literals.

const obj = {
    greet() { return "Hello"; } // Same as greet: function() { return "Hello"; }
};

29. What is deep copy vs shallow copy?

  • Shallow: Top-level copy; nested objects share references
  • Deep: Complete independent copy of all nested objects
// Shallow
const shallow = { ...obj };

// Deep (JSON method — loses functions, dates, undefined)
const deep = JSON.parse(JSON.stringify(obj));

// Deep (modern)
const deep = structuredClone(obj);

30. What is structuredClone()?

A modern built-in function that creates a deep clone of an object. Handles more types than JSON: Dates, Maps, Sets, ArrayBuffers, etc.

31. What is the difference between mutable and immutable objects?

  • Mutable: Can be changed after creation (objects, arrays)
  • Immutable: Cannot be changed (primitives: strings, numbers, booleans)

32. What is object destructuring?

Extracts properties from objects into variables.

const { name, age = 25, email: contact } = user;

33. What is the in operator for objects?

Checks if a property exists in an object (including prototype chain).

console.log("toString" in {}); // true (inherited)
console.log("name" in { name: "A" });  // true (own)

34. What is the delete operator?

Removes a property from an object. Returns true if successful (even if property didn't exist).

delete obj.property;

35. What is the difference between Object.keys() and Object.getOwnPropertyNames()?

  • Object.keys() — own enumerable string-keyed properties
  • Object.getOwnPropertyNames() — all own string-keyed properties (enumerable + non-enumerable)
  • Object.getOwnPropertySymbols() — own symbol-keyed properties