Module 11: JavaScript Interview Questions and Answers
Objects, Arrays, and Prototype Interview Questions
Explain object references, copying, methods, prototypes, and collection choices.
1. Are arrays objects in JavaScript?#
Answer: Yes. Arrays are specialized objects with indexed elements and a length property. Use Array.isArray(value) to check whether a value is an array.
2. What is the difference between map and forEach?#
Answer: map builds and returns a new array from callback results. forEach runs a callback for each item and returns undefined; it is often used for side effects.
3. What is the difference between filter and find?#
Answer: filter returns an array of every matching item. find returns the first matching item or undefined when none match.
4. What is destructuring?#
Answer: Destructuring extracts values from arrays or properties from objects into variables. It supports defaults, renaming, and nested patterns, but complex nested destructuring can reduce readability.
5. What does spread syntax do?#
Answer: Spread expands iterable values or enumerable object properties into a new expression. It is often used to combine arrays and make shallow copies of arrays or objects.
6. What is a shallow copy?#
Answer: A shallow copy creates a new outer object or array but keeps references to nested objects. Changes to a nested object can therefore be visible through both the original and the copy.
7. What is a prototype?#
Answer: A prototype is an object used in property lookup delegation. When a property is not found on an object itself, JavaScript searches its prototype chain.
8. Are JavaScript classes the same as classes in Java?#
Answer: No. JavaScript class syntax provides a structured way to create objects and prototype-based methods. JavaScript remains prototype-based and dynamically typed, while Java has a different class and type model.
9. When should you use Map instead of an object?#
Answer: A Map is useful for dynamic key-value collections, keys of different types, predictable insertion-order iteration, and a direct size property. Plain objects are convenient records with known property names.
10. What is a Set?#
Answer: A Set stores unique values. It is useful for removing duplicates and checking membership without manually searching an array each time.
11. What does optional chaining do?#
Answer: ?. returns undefined instead of throwing when the value immediately before the chain is nullish. It does not validate the entire data structure or make every missing value acceptable.
12. What is the difference between JSON and a JavaScript object?#
Answer: JSON is a text data format with a restricted set of values. A JavaScript object is a runtime value that can contain functions, symbols, undefined, prototypes, and other features not directly represented in JSON.