DEV Community

Fazlay Rabbi
Fazlay Rabbi

Posted on

Common Mistakes JS developer make while using map()

In JavaScript there are few in-built looping functions, map() is one of the most used among them. map() allows maintaining the sanity of your code and avoiding writing the big loops. If you are a react dev than you might use it daily. In this article I’m focused the comma mistakes we do while using it in react. Before going to common mistakes,
let’s visit some common use cases of map () function

  1. To loop over an Array : For some reason if you need to iterate over an array map() will be your best friend Image description

Chaining function to optimize code

Image description

If you want to iterate and apply some other function at the same time you can do it via map

To know about the performance of map vs other looping function you can check
https://leanylabs.com/blog/js-forEach-map-reduce-vs-for-for_of/

Common Mistakes

Like other map will not run over any undefined or null
For that you should check first whether it is valid state or not

Return from the array

While iterating over an array and show particular object in the DOM , either use first bracket or use return

Image description

Top comments (0)