The filter() method in JavaScript is used to create a new array with all elements that pass the test implemented by the provided function. The function takes in two arguments: the current element and its index in the array. It returns true if the element should be included in the new array, and false if it should not.
Here is an example of using the filter() method to filter out all odd numbers from an array:
let numbers = [1, 2, 3, 4, 5, 6];
let evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
console.log(evenNumbers); // Output: [2, 4, 6]
You can also use arrow function instead of function to make it more concise.
let numbers = [1, 2, 3, 4, 5, 6];
let evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers); // Output: [2, 4, 6]
Note that filter() does not change the original array.