JavaScript — Keep it simple! - Part 1

Sanksshep Mahendra
Nov 1 · 3 min read

JavaScript, adds life to your code and helps users interact with your site in an efficient manner. You can do a lot with JavaScript nowadays, it has grown by leaps and bounds since I started using it.

I am excited to write my first article on JavaScript. This article requires you to know the basics of JavaScript, I hope you enjoy reading it.

.map() JS

In this article, we will be discussing some powerful functions in JavaScript.

.map()

.map()

A map holds the values of all key-value pairs, in the order of insertion and it returns any value, either the key or the value.

How does this work?

Let’s say you need to parse a large array containing multiple objects to get just one key-value across the entire array.

You have an array of sale items, but you only need to display the price.

How can you achieve this? There are many ways of doing this, we can use , , or .

Using :

You may have noticed to achieve this we had to create an empty array to push the results. Let's use and see what the code looks like —

If you look at the code above, we have written less code to do more.

We can be even more concise with arrow functions (requires ES6 support, Babel or TypeScript)

Even less code for the same result! Yay!

I believe a small piece of code that gives us the required results is good code, you do not need to write lengthy pieces of code to execute and give you the same result when a single line of code can produce it.

Why?

.map() helps you avoid loops which are more process heavy and can have a direct impact on performance. Nested loops are sometimes necessary, but should be avoided as much as possible. You can now do a lot more with less. Less is more!

Enjoy using .map() in JavaScript.

If you enjoyed reading this, please find other articles I wrote below —

JavaScript in Plain English

Learn the web's most important programming language.

Sanksshep Mahendra

Written by

I am a technology executive with success driving the strategy, design, and execution of software engineering with AI and ML.

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade