Array Flatten in JavaScript

Ever opened a delivery only to find a small box inside a medium box, which is inside a large box? In programming, we call this "nesting." While nested arrays are great for storing complex data, they can be a total headache when you just want to list everything out and work with it.
Today, we’re going to learn how to break those boxes open and get straight to the data.
Here is what we’ll cover:
What nested arrays actually look like
Why we bother flattening them in the first place
The core concept of "flattening"
Different coding approaches (from easy to "interview-level")
Common interview scenarios you might face
What are Nested Arrays?
A nested array (often called a multi-dimensional array) is simply an array that contains other arrays as its elements.
Think of a regular array as a single-row egg carton. A nested array is like a crate containing multiple cartons.
// A simple nested array
const nested = [1, [2, 3], [4, [5, 6]]];
In this example, 5 and 6 are "two levels deep." This structure is common when dealing with complex data like folder hierarchies or organizational charts.
Why Flattening is Useful
You might be wondering: "If the data is already there, why change it?"
Flattening makes your life easier because:
Easier Searching: It’s much simpler to find if a value exists in a single list than to write loops within loops to check every "inner" array.
Data Processing: Most UI components (like a simple list or a table) expect a flat list of items to display.
API Consistency: Sometimes APIs send back data in strange, nested formats that you need to "clean up" before using in your frontend.
The Concept of Flattening
Flattening is the process of reducing the "depth" of an array.
Shallow Flatten: Reducing the depth by just one level.
Deep Flatten: Reducing the depth until every single element is on the top level, no matter how many boxes they were wrapped in.
Different Approaches to Flatten Arrays
The Modern Way: flat()
If you are working in a modern environment, JavaScript has a built-in method called .flat(). By default, it flattens one level deep, but you can tell it how deep to go.
const arr = [1, [2, [3, 4]]];
console.log(arr.flat()); // [1, 2, [3, 4]] (1 level)
console.log(arr.flat(2)); // [1, 2, 3, 4] (2 levels)
console.log(arr.flat(Infinity)); // Flattens everything!
The Interview Way: Recursion
In interviews, you might be asked to write your own flattening function without using the built-in method. This is where recursion comes in. The logic is: "Look at each item. If it's an array, flatten it. If it's not, keep it."
function deepFlatten(arr) {
let result = [];
arr.forEach(item => {
if (Array.isArray(item)) {
// If it's an array, we "dig deeper"
result = result.concat(deepFlatten(item));
} else {
// If it's a value, just add it to our list
result.push(item);
}
});
return result;
}
Common Interview Scenarios
If you're heading into a technical interview, be ready for these variations:
"Manual" Flattening: They want to see if you understand recursion or how to use
reduceandconcat.Depth Control: They might ask you to flatten an array exactly
ntimes.Performance: For massive arrays, recursion can sometimes cause a "stack overflow." Interviewers love it when you mention that an iterative approach (using a
whileloop and a stack) might be safer for huge datasets.
Conclusion
Flattening arrays is a fundamental skill that moves you from "just writing code" to "organizing data efficiently." Whether you use the quick .flat() method for your daily projects or master the recursive approach for your next big interview, understanding how to manipulate data structures is what sets a great developer apart.

