Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Arrow Functions | Modern and Advanced Function Patterns
Functions in JavaScript

bookArrow Functions

Pyyhkäise näyttääksesi valikon

Arrow functions offer a modern, concise way to write functions in JavaScript. They are especially useful for short, simple tasks, such as working with arrays or creating small utility functions.

The syntax for arrow functions is more compact than traditional function expressions. To define an arrow function, you use the => syntax.

For example, a function that takes a number and returns its double can be written as:

const double = (n) => n * 2;

If a function has only one parameter, you can omit the parentheses:

const double = n => n * 2;

If the function body is a single expression, the result is returned automatically, so the return keyword is not needed.

const add = (a, b) => a + b;
123456789101112131415
// Regular function function doubleArray1(arr) { return arr.map(function (num) { return num * 2; }); } // Usage: console.log(doubleArray1([1, 2, 3])); // [2, 4, 6] // Arrow function const doubleArray2 = (arr) => arr.map((num) => num * 2); // Usage: console.log(doubleArray2([1, 2, 3])); // [2, 4, 6]
copy
  • Arrow functions provide a shorter syntax for writing functions;
  • Parentheses can be omitted when there is only one parameter;
  • Single-expression functions return values automatically;
  • Arrow functions are best used for small, simple operations.
question mark

Which of the following is a correct ARROW function that takes one parameter x and returns x squared?

Valitse kaikki oikeat vastaukset

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 3. Luku 2
some-alt