map return javascript code example
Example 1: how the map function works javascript
const array = [2, 5, 9];
let squares = array.map((num) => num * num);
console.log(array); // [2, 5, 9]
console.log(squares); // [4, 25, 81]
Example 2: map javascript
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]. numbers still [1, 4, 9]
Example 3: how to use the map method in javascript
const numbers = [1, 2, 3, 4, 5];
const bigNumbers = numbers.map(number => {
return number * 10;
});