JavaScript – Filter Even Numbers of Integer Array
To filter even numbers of an integer Array in JavaScript, call Array.filter() method on this integer array, and pass a function as argument that returns true for an even number or false otherwise. filter() method returns an array with elements from the original array that returns true for the given callback function.
Example
In the following script, we take an integer array arr
, and filter even numbers in this Array. We pass a lambda function to the filter() method.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var arr = [2, 5, 7, 8, 10, 13, 16];
var result = arr.filter((n) => n % 2 == 0)
document.getElementById('output').innerHTML = 'Resulting Array : ' + result;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we have learnt how to use Array.filter() method to filter even numbers of an integer Array with examples.