Arrow Function Example
suggest changeLet’s consider this example, that outputs the squares of the numbers 3, 5, and 7:
let nums = [3, 5, 7]
let squares = nums.map(function (n) {
return n * n
})
console.log(squares)
The function passed to .map can also be written as an arrow function by removing the function keyword and adding the arrow =>:
let nums = [3, 5, 7]
let squares = nums.map((n) => {
return n * n
})
console.log(squares)
This can be written even more concisely. If the function body is a single expression, the curly braces wrapping the function body can be removed, as well as the return keyword.
let nums = [3, 5, 7]
let squares = nums.map(n => n * n)
console.log(squares)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents