JS Built-ins
← Array24 / 226
ArrayArray.isArrayintermediate

Array.isArray() β€” filter only arrays

πŸ“– Usage Example
const mixed = [1, 'a', [2, 3], {b: 4}, [5]]
mixed.filter(Array.isArray)  // β†’ [[2, 3], [5]]

Pass Array.isArray as a filter callback to extract only the array elements from a mixed collection.

Array.isArray() β€” filtering a mixed collection

Array.isArray is often used as a predicate in .filter() to pull only array values out of a mixed collection.

Challenge: Implement keepOnlyArrays that takes an array of unknown values and returns only the elements that are themselves arrays.

keepOnlyArrays([1, [2], 'hi', [3, 4], null]) // β†’ [[2], [3, 4]]
keepOnlyArrays([1, 2, 3])                    // β†’ []
πŸ’‘ Hints (2)
  • β€’ `Array.isArray` is a function β€” you can pass it directly to `.filter()` without wrapping.
  • β€’ The return type is `unknown[][]` because the kept elements are themselves arrays.