Flatten Array

Difficulty: easy

Write flattenArray(arr), a function that takes a nested array and returns a new one-dimensional array.

Think of the array like boxes inside boxes. If an item is a normal value, put it into the answer. If an item is another array, open it and repeat the same process until no arrays are left.

Requirements

  • Preserve the left-to-right order of values.
  • Support any nesting depth.
  • Return a new array. Do not mutate the input array.

More JS Coding

View all JS Coding →
JS CodingMedium

5. Throttle

JS CodingMedium

13. Deep Clone

JS CodingMedium

14. Event Emitter

JS CodingEasy

66. Promise Methods

JS CodingEasy

19. Memoize I

JS CodingMedium

86. API Retry

Related hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub

FAQ

Why is Flatten Array a popular JS interview question?

It tests recursion vs stack-based approaches, depth control, and how you reason about nested data structures under time pressure.

What variants should I prepare for?

Fully flatten, flatten by depth N, and preserving non-array values — interviewers often add a depth constraint as a follow-up.

Related practice?

Deep clone, reduce polyfill, and currying utilities.