JavaScript Course
JavaScript
/
Intermediate

Implement: Flatten Array

Definition

A highly common interview coding question: Write a function that takes an arbitrarily deeply nested array and flattens it into a 1D array without using the native `Array.flat()` method.

Explain Like I'm New

The interviewer wants to see if you understand Recursion and type checking (`Array.isArray`).

Real World Example

Flattening `[1, [2, [3, 4], 5]]` into `[1, 2, 3, 4, 5]`.

Common Use Cases

  • •Recursion mastery
  • •Interview screening

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • How do you check if a variable is an array in JS?

intermediate

  • Write a recursive solution to flatten an array.

advanced

  • How would you flatten an array iteratively (without recursion) using a Stack?

Flash Cards

Question

How do you check if a variable is an array?

Click to reveal answer
Answer

`Array.isArray(variable)`. Using `typeof` will return `'object'`, which is not helpful.