Tutorial

map() Array Method in JavaScript

JavaScript

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.

Way cooler than using for loops on arrays, map() iterates over an array and creates a new array that has had each item passed through a function:

var drinks = [“coffee soda”, “tea”, “whiskey”];

var coldDrinks = drinks.map(function(drink) {
    return ‘iced ’ + drink;
});

// [‘iced coffee soda’, ‘iced coffee’, ‘iced whiskey’]

The syntax is very simple:

var newArray = myArray.map(function);
Creative Commons License