← All Exercises

Flatten Nested Array

basicjavascript~15min

Background

Array flattening is essential when processing tree-like data structures or normalizing API responses with nested arrays.

Problem

Write a function that takes an array which may contain nested arrays of arbitrary depth and returns a fully flattened array.

Input

An array that may contain numbers, strings, or nested arrays at any depth.

Output

A flat array with all nested elements extracted to a single level.

Sample tests

Sample 1

Input: [1, [2, [3, [4]]]]
Expected: [1, 2, 3, 4]

Your solution

Learning objective

Learn recursive array processing and understand depth handling in flattening.