Skip to main content
πŸ” Recursion

Is It an Array?

The next challenge is flattening a nested array, where an item can be a number or another array. Before we recurse into it, we need one small tool: how to ask "is this item an array?"

JavaScript gives you exactly that β€” Array.isArray:

Array.isArray([1, 2]); // true
Array.isArray(7);      // false
Array.isArray("hi");   // false

This is the decision that drives a flatten: if an item is an array, we'll recurse into it; if it isn't, we keep it as-is. Let's nail the check on its own first, so flatten is one less thing to think about.

Your task

Write shouldRecurse(item) that returns true when item is an array and false otherwise.

⚑ 25 XP

🧱 Code blocks

Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.

solution.jsRuns in your browser
Loading editor…
Tests4 to pass
[1,2] is an array
empty array counts
7 is not an array
"hi" is not an array

Run your code to check these.