Discover the power of JavaScript arrays with advanced methods like reduce, find, some, and every. Learn sorting, filtering, destructuring, and explore nested arrays with flat and flatMap. Dive in!
1. Array Methods: reduce, find, some, every
Which method executes a reducer function on each array element to produce a single output value? a) reduce b) find c) some d) every
What does the find method return when no element satisfies the condition? a) null b) undefined c) An empty array d) false
Which method tests whether at least one array element passes the provided function? a) reduce b) find c) some d) every
The every method checks if: a) Any element meets the condition. b) All elements meet the condition. c) At least one element meets the condition. d) No elements meet the condition.
What does reduce require as its first argument? a) A callback function. b) A condition. c) A boolean. d) An array.
How many parameters does the callback function of reduce accept? a) 1 b) 2 c) 3 d) 4
What happens if you don’t provide an initial value to reduce? a) Throws an error. b) Uses undefined as the initial value. c) Uses the first element of the array. d) Skips the first iteration.
Which method stops searching once it finds the first match? a) reduce b) find c) some d) every
If some finds a match, it immediately returns: a) true b) false c) The matched value. d) An error.
Can every return true for an empty array? a) Yes b) No
2. Sorting and Filtering Arrays
What method sorts the elements of an array in place? a) sort b) filter c) flat d) flatMap
What does sort use to compare elements by default? a) Lexicographical order b) Numerical order c) Boolean values d) Random order
Which method creates a new array with elements that pass a specific condition? a) filter b) sort c) flat d) flatMap
What is the return type of filter when no elements satisfy the condition? a) null b) An empty array c) undefined d) The original array
Which method modifies the original array? a) filter b) sort c) Both d) Neither
How can you sort numbers in ascending order using sort? a) Pass a callback (a, b) => a - b. b) Pass a callback (a, b) => b - a. c) Use filter instead of sort. d) Use flat.
What type of method is filter? a) Mutating b) Non-mutating
3. Array Destructuring
What does array destructuring allow you to do? a) Clone an array. b) Assign array values to variables. c) Sort array elements. d) Flatten nested arrays.
Which syntax correctly destructures the first two elements of an array? a) {a, b} = array b) [a, b] = array c) (a, b) = array d) <a, b> = array
What happens if the array has fewer elements than the destructured variables? a) Throws an error. b) Assigns undefined to extra variables. c) Skips destructuring. d) Stops execution.
How can you skip elements during destructuring? a) Use commas without variables. b) Use null. c) Use an empty object. d) Use undefined.
Destructuring can be used with: a) Only arrays. b) Only objects. c) Arrays and objects.
4. Nested Arrays and flat, flatMap Methods
What does the flat method do? a) Sorts the array. b) Flattens nested arrays. c) Filters the array. d) Maps each element.
What parameter does flat accept? a) A callback function. b) A depth level. c) A sorting condition. d) An array.
What is the default depth for the flat method? a) 1 b) 2 c) Infinity d) 0
Which method combines mapping and flattening? a) filter b) flat c) flatMap d) reduce
What is a key difference between flat and flatMap? a) flat modifies the original array. b) flatMap performs a mapping before flattening. c) flatMap works on objects only. d) flat sorts the array.
Which method is more efficient for combining map and flat? a) map followed by flat b) flatMap
What happens if you call flat(Infinity) on a deeply nested array? a) Flattens completely. b) Throws an error. c) Returns the original array. d) Only flattens once.
Can flatMap alter the number of elements in the array? a) Yes b) No