Arrow Function Example

suggest change

Let’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)

Run in RunKit

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)

Run in RunKit

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)

Run in RunKit

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents