fts_acer fts_acer - 1 month ago 6
Javascript Question

Why doesn't foreach() work on filter() result array in JS?

I have a simple JavaScript array. I'm running a filter() on it. According to the MDN documentation, filter() returns an array. Also according to MDN, arrays have a function foreach().

However, when I run foreach() on an array returned from filter() (as in the example below), I get:

Uncaught TypeError: evens.foreach is not a function

Here is some test code, tested in browser and on JSFiddle. Why would this not work? How can I make it work?

var nums = [1,2,3,4,5,6,7,8,9,10,11,12];
var evens = nums.filter(function(num){
if(num % 2 == 0){return num;}

// [2, 4, 6, 8, 10, 12]

// print even thirds
evens.foreach( function(even) {
if(even % 3 == 0) {


change evens.foreach to evens.forEach