Usage Guide: Array.prototype.filter()
The filter() method is one of the most powerful tools in modern JavaScript. It is based on the concept of immutability: it does not change the original array, but rather projects a new one.
Syntax:
const newArray = array.filter((element, index, array) => { ... });Key Points:
- Return: If the callback function returns atruthy value, the element is included.
- Chaining: By returning an array, you can use
.map()or.reduce()immediately after. - Immutability: It is preferable in frameworks like React to avoid side effects.