forEach () executes the callback function once for each array element; unlike map() or reduce() it always returns the value undefined and is not chainable. The typical use case is to execute side effects at the end of a chain.
Others already mentioned some stuff, but if you really want to use forEach here, you need to know that this method can actually pass 2 arguments to your function, not just one, with second argument storing current element index like this:
array.forEach(function(n,index){
if (n === element) console.log(index);
});
This is bad solution because it executes method “indexOf” twice, basically making search twice for same thing.
Consider this comparison:
find is your function. find2 is same function, but instead of running indexOf twice, it stores result and then uses it.
You will see that this approach will be reliably faster.
In other words, if you are executing some calculating function once, and need to use result in multiple areas, just store result in variable, do not re execute function for no reason.