Javascript - Map functional programming function

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

The functional programming function map in a Javascript context is only an array method.

Advertising

3 - Example

3.1 - Arrow Function

With an Arrow function as argument

  • An array of number that we want to double.
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((number) => number * 2);
console.log(doubled);

3.2 - Function Declaration

With an Javascript - Function Declaration

  • The function declaration
function double(num) {
    return 2*num;
}
  • The array of number that we want to double.
const numbers = [1, 2, 3, 4, 5];
// Passing the function name
const doubled = numbers.map(double);
// Declaring the function inside the map works also
const doubled2 = numbers.map(function double(num) {
    return 2*num;
});
console.log("doubled data");
console.log(doubled);
console.log("doubled2 data");
console.log(doubled2);

Advertising

4 - Documentation / Reference

web/javascript/es/map.txt · Last modified: 2019/06/18 11:06 by gerardnico