Interview Questions
Objects · Study material
JavaScript Objects – Interview Questions
1. What is the difference between dot and bracket notation?
Dot notation needs a literal, valid identifier (obj.name). Bracket notation takes any string or expression (obj["first-name"], obj[key]).
2. How do you check whether an object has a property?
Object.hasOwn(obj, key) (or obj.hasOwnProperty(key)) for own properties; key in obj also checks the prototype chain.
3. What is the difference between Object.freeze and Object.seal?
freeze makes every own property read-only and blocks adding or deleting; seal blocks adding and deleting but allows changing existing values. Both are shallow.
4. How do you copy an object?
Shallow: { ...obj } or Object.assign({}, obj). Deep: structuredClone(obj). Assignment copies only the reference.
5. What is this inside an object method?
The object the method was called on (the value before the dot). Detached calls lose it: undefined in strict mode, the global object otherwise. Arrow functions do not have their own this.
6. What do Object.keys, Object.values and Object.entries return?
Arrays of the object's own enumerable string-keyed property names, values, or [key, value] pairs.
7. What happens when you use an object as a key in another object?
It is converted to the string "[object Object]", so all object keys collide. Use a Map or WeakMap.
8. What are getters and setters?
Accessor properties: a get function runs when the property is read and a set function when it is assigned – used for computed values and validation.
9. What is a property descriptor?
The attributes of a property: value, writable, enumerable, configurable (or get/set). Read with Object.getOwnPropertyDescriptor, set with Object.defineProperty.
10. What is the prototype chain?
The linked series of prototype objects JavaScript searches when a property is not found on the object itself; it ends at Object.prototype and then null.
11. What does Object.create(null) create?
An object with no prototype – no toString, hasOwnProperty or other inherited keys – useful as a pure dictionary.
12. What does JSON.stringify do with undefined, functions and Dates?
undefined and function properties are omitted (null inside arrays), Dates become ISO strings, NaN/Infinity become null, BigInt and circular references throw.
13. How do you compare two objects for equality?
=== checks identity. For content, compare keys and values recursively (a deep-equal function) – JSON.stringify comparison breaks with different key orders.
14. What is the difference between spread and Object.assign?
Both copy own enumerable properties shallowly. Object.assign mutates its target and triggers setters on it; spread always creates a new object.
15. In what order are object keys listed?
Integer-like keys in ascending order first, then string keys in insertion order, then symbols (only by getOwnPropertySymbols / Reflect.ownKeys).