3. What is the differences between map(),
filter(), forEach(), and
find() in JavaScript?
Some of the pre-built functions can have very similar use cases to
one another, therefore I have created a list of manipulation
functions, with how they look and their use cases. another form of
just a plain for loop that can be used to iterate
.map() Function
Function .map() is a manipulative function that can
modify each element's content in an array that it is called
on. This function returns a new array with modified values, Just
like the function .forEach(), this function also
takes only 1 parameter .map(callback). The parameter
callback can take up to 3 parameters
element, index & array (but
only the element parameter is required, the rest is
optional).
.filter() Function
Function .filter() is a search function that returns all the
elements that fulfil the assigned condition. Just like the
function .forEach(), this function also takes only 1
parameter and The parameter callback can take up to 3
parameters.
.forEach() Function
.forEach() is another form of just a plain for loop
that can be used to iterate through array items. This function
takes 1 parameter .forEach(callback) . The parameter
callback can take up to 3 parameters
element , index &
array (but only the element parameter
is required, the rest is optional).
.find() Function
Function .find() is also a search function like the
previous but they differ in one small detail — this function
returns only one match in an array. ust like the function
.forEach(), this function also takes only 1 parameter
and The parameter callback can take up to 3
parameters.
Conclusion
Creating complex functions that are run only once is not the best
way to treat your code, instead, you can use these array functions
that can be evaluated more easily.