Flatten an array of arbitrarily nested arrays of integers
The check for arr.length &gt; 0 is not needed: forEach() can be called
on an empty array (and does nothing in that case).

The check if (arr) protects against a call with an undefined argument,
but not against a call with a non-array. As an example, flatten(123)
would cause a JavaScript error. So you have to decide:


If the argument to flatten() is known to be an array then the
outer if statement is not needed at all.
If flatten() can be called with any argument than you need to check
for an array before calling forEach().


See also Flatten an array on how reduce() can be used instead of a loop.