
Are you using map function in a long code write?
Sometimes you dont know that you are using map function in a long code write
<p>A common mistake often seen in beginners learning JavaScript or those starting to work with JS is using incorrectly name of functions.</p> <p>In this article, I will be working with three topics: <strong>map</strong>, <strong>name functions</strong>, and <strong>callbacks</strong>.</p> <p>An example of modifying an array: To simulate the process of a common mistake, I will start with the simplest array:</p> <pre><code>let clubNames = 'Manchester United', 'Chelsea', 'Arsenal'; </code></pre> <p>Now, I want add a string "<strong>Club</strong>" at the end of each club name. This is what we usually do:</p> <pre><code>clubNames.map(name => ${name} club); </code></pre> <p>Then we get the following result:</p> <pre><code>"Manchester United club", "Chelsea club", "Arsenal club" </code></pre> <p>So now how can you use naming function for this case? I guess you would do it usually like this:</p> <pre><code>function addTermToClubNames (club){ return ${player} club } clubNames.map(club => addTermToClubNames(club)) </code></pre> <p>However, this is a long code. Better to change using callback function with the following line of code:</p> <pre><code>clubNames.map(addTermToClubNames) </code></pre> <p>Simple isn't it, I believe this is something that few people notice and often don't take advantage of!</p>