JavaScript Array Methods 1 — Questions and Answers
Question 1: What does `Array.prototype.map()` return?
- The original array modified in place
- A new array with each element transformed by the callback (Correct answer)
- A boolean indicating whether all elements passed a test
- The first element that satisfies a condition
Correct answer: A new array with each element transformed by the callback
`map()` creates and returns a new array populated with the results of calling the provided callback function on every element in the original array. The original array is not modified.
`map(callback)` calls `callback(element, index, array)` for each element and builds a new array from the return values. It's a pure transformation — always returns a new array of the same length. If you don't need the result (just side effects), use `forEach` instead. `filter()` returns a subset, `reduce()` returns a single value.
Question 2: What does `Array.prototype.reduce()` do?
- Removes duplicate values from an array
- Applies a function against an accumulator and each element to reduce the array to a single value (Correct answer)
- Splits an array into chunks
- Converts an array to an object
Correct answer: Applies a function against an accumulator and each element to reduce the array to a single value
`reduce()` executes a reducer function on each element of the array, accumulating the result into a single output value. It takes a callback and an optional initial value.
`array.reduce((accumulator, currentValue, index, array) => newAccumulator, initialValue)`. If `initialValue` is omitted, the first array element is used as the initial accumulator. Uses: summing numbers, flattening arrays, building objects from arrays, counting occurrences. `reduceRight()` works from right to left.
Question 3: What is the difference between `Array.prototype.find()` and `Array.prototype.filter()`?
- `find()` returns all matches; `filter()` returns the first match
- `find()` returns the first matching element; `filter()` returns an array of all matches (Correct answer)
- They are identical
- `find()` searches by index; `filter()` searches by value
Correct answer: `find()` returns the first matching element; `filter()` returns an array of all matches
`find()` returns the first element that satisfies the testing function, or `undefined` if none found. `filter()` returns a new array containing ALL elements that satisfy the test.
`find()` short-circuits on the first match, making it more efficient when you only need one element. `filter()` always iterates the entire array. `findIndex()` is the sibling of `find()` that returns the index instead of the value. `some()` is similar to `find()` but returns a boolean instead of the value.
Question 4: What does `Array.prototype.flat()` do?
- Sorts the array in ascending order
- Creates a new array with sub-arrays concatenated into it up to a specified depth (Correct answer)
- Converts the array to a flat string
- Removes falsy values from the array
Correct answer: Creates a new array with sub-arrays concatenated into it up to a specified depth
`flat()` creates a new array with all sub-array elements concatenated into it recursively up to the specified depth (default: 1). `flat(Infinity)` flattens all nesting levels.
`[1, [2, [3]]].flat()` → `[1, 2, [3]]` (depth 1). `[1, [2, [3]]].flat(2)` → `[1, 2, 3]`. `[1, [2, [3]]].flat(Infinity)` → `[1, 2, 3]`. `flatMap()` is a combination of `map()` then `flat(1)` and is efficient for operations that produce arrays from each element.
Question 5: What does `Array.prototype.some()` return?
- The first element that passes the test
- An array of elements that pass the test
- true if at least one element passes the test, otherwise false (Correct answer)
- The number of elements that pass the test
Correct answer: true if at least one element passes the test, otherwise false
`some()` tests whether at least one element in the array passes the test implemented by the provided function. It returns `true` immediately when a matching element is found, `false` if none match.
`some()` is the existential quantifier — 'does any element satisfy this?' Its counterpart `every()` is the universal quantifier — 'do all elements satisfy this?' Both short-circuit: `some()` stops at the first `true`, `every()` stops at the first `false`. For empty arrays, `some()` returns `false` and `every()` returns `true` (vacuous truth).
Question 6: How does `Array.prototype.splice()` differ from `Array.prototype.slice()`?
- They are identical methods with different names
- `splice()` modifies the original array; `slice()` returns a new sub-array without modifying the original (Correct answer)
- `slice()` modifies the original; `splice()` does not
- `splice()` only removes elements; `slice()` only adds elements
Correct answer: `splice()` modifies the original array; `slice()` returns a new sub-array without modifying the original
`splice()` modifies the original array by removing, replacing, or adding elements in place. `slice()` returns a shallow copy of a portion of an array as a new array without modifying the original.
`splice(start, deleteCount, ...itemsToAdd)` mutates the array and returns the removed elements. `slice(start, end)` returns a new sub-array from `start` (inclusive) to `end` (exclusive), the original is unchanged. Remember: splice = mutate, slice = copy. Both accept negative indices to count from the end.
What does `Array.prototype.map()` return?