JavaScript – get array element fulfilling a condition

In most browsers (not IE <= 8) arrays have a filter method, which doesn’t do quite what you want but does create you an array of elements of the original array that satisfy a certain condition:

function isGreaterThanFive(x) {
     return x > 5;
}

[1, 10, 4, 6].filter(isGreaterThanFive); // Returns [10, 6]

Mozilla Developer Network has a lot of good JavaScript resources.

Leave a Comment