JavaScript Object Methods 1 — Questions and Answers
Question 1: What does `Object.keys()` return?
- All properties including inherited ones
- An array of an object's own enumerable property names (Correct answer)
- An array of the object's values
- An array of [key, value] pairs
Correct answer: An array of an object's own enumerable property names
`Object.keys()` returns an array of a given object's own enumerable string property names (keys). It does not include inherited properties or non-enumerable properties.
`Object.keys()`, `Object.values()`, and `Object.entries()` are the trio of object iteration methods. `keys()` returns `string[]`, `values()` returns the corresponding values, `entries()` returns `[key, value][]`. None of them include Symbol keys (use `Object.getOwnPropertySymbols()` for those) or inherited properties (use `for...in` for inherited enumerable ones).
Question 2: What does `Object.assign()` do?
- Creates a deep clone of an object
- Copies all enumerable own properties from one or more source objects to a target object (Correct answer)
- Assigns a prototype to an object
- Seals an object against modification
Correct answer: Copies all enumerable own properties from one or more source objects to a target object
`Object.assign(target, ...sources)` copies all enumerable own properties from source objects into the target object. It returns the target. This is a shallow copy — nested objects are referenced, not cloned.
`Object.assign({}, obj1, obj2)` merges obj1 and obj2 into a new object. If source objects have the same key, later sources overwrite earlier ones. It's a shallow copy: `const copy = Object.assign({}, original)` — modifying `copy.nested` also affects `original.nested`. For deep cloning, use `structuredClone()` (modern) or `JSON.parse(JSON.stringify(obj))` (with limitations).
Question 3: What does `Object.freeze()` do?
- Prevents new properties from being added but allows modification of existing ones
- Prevents all modifications — adding, deleting, or changing property values (Correct answer)
- Creates an immutable deep clone of the object
- Prevents the object from being garbage collected
Correct answer: Prevents all modifications — adding, deleting, or changing property values
`Object.freeze()` prevents new properties from being added, existing properties from being removed, and existing property values from being changed. It makes the object effectively immutable (shallowly).
A frozen object cannot be modified in strict mode (throws TypeError). In sloppy mode, modifications silently fail. `Object.freeze()` is shallow: `const obj = Object.freeze({ nested: { x: 1 } })` — you cannot reassign `obj.nested`, but you can still change `obj.nested.x = 2`. For deep immutability, recursively freeze all nested objects. `Object.isFrozen()` checks the state.
Question 4: What does `Object.entries()` return?
- An array of the object's own enumerable property values
- An array of [key, value] pairs for own enumerable properties (Correct answer)
- A Map of the object's properties
- An array of property descriptors
Correct answer: An array of [key, value] pairs for own enumerable properties
`Object.entries()` returns an array of a given object's own enumerable string-keyed property `[key, value]` pairs. It's useful for iterating over objects with both key and value.
`Object.entries({ a: 1, b: 2 })` → `[['a', 1], ['b', 2]]`. Combined with destructuring: `for (const [key, val] of Object.entries(obj)) { ... }`. You can also create a Map: `new Map(Object.entries(obj))`. The reverse operation — creating an object from entries — is `Object.fromEntries([['a', 1], ['b', 2]])` → `{ a: 1, b: 2 }`.
Question 5: What is the difference between `Object.seal()` and `Object.freeze()`?
- They are identical
- `seal()` prevents adding/deleting properties but allows changing existing values; `freeze()` prevents all changes (Correct answer)
- `freeze()` allows value changes; `seal()` prevents all changes
- `seal()` is for arrays; `freeze()` is for objects
Correct answer: `seal()` prevents adding/deleting properties but allows changing existing values; `freeze()` prevents all changes
`Object.seal()` prevents adding new properties and deleting existing ones, but allows changing the values of existing properties. `Object.freeze()` additionally prevents changing existing property values.
Both `seal()` and `freeze()` set `configurable: false` for all properties and prevent new property additions. But `freeze()` also sets `writable: false` for all data properties. Think of `seal()` as 'lock the shape but allow edits' and `freeze()` as 'complete read-only'. `Object.isSealed()` and `Object.isFrozen()` check these states.
Question 6: What does `Object.create()` do?
- Creates a plain empty object like `{}`
- Creates a new object with the specified object as its prototype (Correct answer)
- Copies an object's properties to a new object
- Defines properties on an existing object
Correct answer: Creates a new object with the specified object as its prototype
`Object.create(proto)` creates a new object and sets its prototype to the provided object. This allows you to explicitly control the prototype chain of new objects.
`const animal = { breathe() { return true; } }; const dog = Object.create(animal);` — `dog` inherits `breathe` from `animal`. `dog.__proto__ === animal` is `true`. `Object.create(null)` creates an object with NO prototype (useful for pure hashmaps). The second argument lets you define properties: `Object.create(proto, { prop: { value: 1, writable: true } })`.
What does `Object.keys()` return?