I think it's helpful to understand why, regardless of what the forEach method actually does, the above couldn't work.
Even if we imagine that forEach short circuits on a truthy return value from the function passed in... and returns that value...
There's still no way that value would make it to the output of our greaterThan function.
This would work (if that's what forEach did), since we are taking note of the return value. (The || false is there so that we know exactly what we're returning, not null or undefined)
Since the .findIndex().find().some() family of methods do exist, we can indeed do that:
I think it's helpful to understand why, regardless of what the
forEachmethod actually does, the above couldn't work.Even if we imagine that
forEachshort circuits on a truthy return value from the function passed in... and returns that value...There's still no way that value would make it to the output of our
greaterThanfunction.This would work (if that's what
forEachdid), since we are taking note of the return value. (The|| falseis there so that we know exactly what we're returning, notnullorundefined)Since the
.findIndex().find().some()family of methods do exist, we can indeed do that:Functions that have only one statement - a return statement, are also prime candidates to be arrow functions:
Finally, if doing this in real life, you probably want to avoid iterating on every check if the array is unchanged: