Use map() method in JavaScript Array

Shiv Kumar Ganesh
Aug 9, 2017 · 1 min read

map() method helps you to create a new array by calling a function on each element of the Array. This function is a custom function which can be used in order to manipulate data within an array and get a new resultant array.

A simple implementation of map() over an Array. Let’s find the square of all the possible numbers in an array.

ES5 Implementation

var numbers = [3,4,22,533,6,52,1,34,454,2,435,456];

var numberSquareES5 = numbers.map(function(number){
return number*number;
});
console.log(numberSquareES5)

ES6 Implementation

var numbers = [3,4,22,533,6,52,1,34,454,2,435,456];

var numberSquareES6 = numbers.map(number=>number*number);
console.log(numberSquareES6)

This can be seen in action out here below.

Check out all the other posts on JavaScript Essentials to get a Grasp of these JavaScript Utilities.


Originally published at The Web Juice.

Shiv Kumar Ganesh

Written by

Interested in friends and am a Web Developer. Design Websites and Web Solutions in major Platforms, SEO Consultant

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