For example, let's say that we have an array of numbers, and that we would like to create a new array consisting of only the even numbers from our original array.
You could write out a whole function involving a for loop, some conditional logic, and pushing elements into a new array. However, I prefer to use .filter() because there is less code to write, and the syntax is much cleaner.
To get back a new array of only the even numbers from our original array you simply write:
And that's all there is to it. You can also use .filter() on an array of objects, to return a new array of objects containing only those objects that meet the condition that you provide.
Thanks for reading, and happy coding!