by Sai gowtham

How use map method in JavaScript

In es6, we got most useful array methods in JavaScript one of them is Array.map() method in this tutorial we will learn about how to use the map method.

The map method takes the callback function as an argument and runs on each element present in the array, at final it returns the new array with the updated values.

map method doesn’t mutate/change the original array.

Example:

function doubleNumbers(arr){
    return arr.map((num) => num*2 );
}

console.log(doubleNumbers([1,2,3,4,5])) // [2,4,6,8,10]

The callback function inside the map method takes three parameters currentvalue , index , array.

function convertIntegers(arr){

    return arr.map((num)=> Number(num) );

}


console.log(convertIntegers(["1","2","3","4"]))

//output   [1, 2, 3, 4]

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course