Easy Tutorial
❮ Prop Script Type Jsref Fontcolor ❯

JavaScript Array map() Method

JavaScript Array Object

Example

Returns a new array with the square roots of the elements of the original array:

var numbers = [4, 9, 16, 25];
function myFunction() {
  x = document.getElementById("demo");
  x.innerHTML = numbers.map(Math.sqrt);
}

Output result:

2,3,4,5

Definition and Usage

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

The map() method processes the elements in the original array in order.

Note: map() does not execute the function for array elements without values.

Note: map() does not change the original array.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method Chrome IE Firefox Safari Opera
map() Yes 9 1.5 Yes Yes

Syntax

array.map(function(currentValue, index, arr), thisValue)

Parameter Details

Parameter Description
function(currentValue, index, arr) Required. A function to be run for each element in the array. <br>Function arguments: <br>currentValue - Required. The value of the current element. <br>index - Optional. The array index of the current element. <br>arr - Optional. The array object the current element belongs to.
thisValue Optional. A value to be passed to the function to be used as its "this" value. <br>If this parameter is empty, undefined will be passed, and the "this" value inside the function will be the global object.

Technical Details

Return Value: A new array with each element being the result of the callback function.
JavaScript Version: 1.6

More Examples

Example

Multiplies each element in the array by a value specified in an input field and returns a new array:

var numbers = [65, 44, 12, 4];
function multiplyArrayElement(num) {
  return num * document.getElementById("multiplyWith").value;
}
function myFunction() {
  document.getElementById("demo").innerHTML = numbers.map(multiplyArrayElement);
}

JavaScript Array Object

❮ Prop Script Type Jsref Fontcolor ❯