showing results for - "arrow functions basic usages in javascript"
Emilie
29 May 2020
1// An empty arrow function returns undefined
2let empty = () => {};
3
4(() => 'foobar')();
5// Returns "foobar"
6// (this is an Immediately Invoked Function Expression)
7
8var simple = a => a > 15 ? 15 : a;
9simple(16); // 15
10simple(10); // 10
11
12let max = (a, b) => a > b ? a : b;
13
14// Easy array filtering, mapping, ...
15
16var arr = [5, 6, 13, 0, 1, 18, 23];
17
18var sum = arr.reduce((a, b) => a + b);
19// 66
20
21var even = arr.filter(v => v % 2 == 0);
22// [6, 0, 18]
23
24var double = arr.map(v => v * 2);
25// [10, 12, 26, 0, 2, 36, 46]
26
27// More concise promise chains
28promise.then(a => {
29  // ...
30}).then(b => {
31  // ...
32});
33
34// Parameterless arrow functions that are visually easier to parse
35setTimeout( () => {
36  console.log('I happen sooner');
37  setTimeout( () => {
38    // deeper code
39    console.log('I happen later');
40  }, 1);
41}, 1);
42
queries leading to this page
arrow functions jsarrow functions es6 exampleare arrow functions es6fat arrow function in javascriptwhy to use arrow functionsarrow functions as methodshow to declare arrow function in javascript without parameterarrow function callback javascriptarrow functions in javscriptthis in arrow function 2c anonymouse function jswrite arrow function javascriptwhy we can 27t use arrow functions always in javascriptarrow notation javascriptwhat is the use of arrow function in javascriptjs arrow functionarrows function in jsthis with arrow functionsarrow function in jsbind arrow function javascriptarrow function javascript with eventarrow fonction en jswhen can you not use arrow functions in es6when we should use arrow function in javascriptuse arrow funcion in js prototypethis keyword in javascript arrow functionwhen should i use arrow functions in es6 3fget the arguments from arrow function jsjs when not to use arrow functionsjs arrow function no argumentsarrow functios in jsdo arrow functions need returnarrow function examplefat arrow function javascriptwhat is scope of arrow function in javascriptwhat is es6 arrow functionarrow notation in javascriptjavascript arrow syntaxjavascript arrow function without parametersare arrow functions betterarrow function in javascriptis arrowfunctions only in jsuse of arrow function javascriptarrow function syntaxcan i place arrow functions in objects in jsjavascript arrow function and thisdo i have to use arrow functions in node jsarrow function ec6this in arrow function jsarrow function es6js arrow function tutorialarrow function javascriptshould we use functions expression or arrow functions in objects in javascriptarrow function normal way array methods javascriptwhat are arrow functions good forreturning a 28 7b a 7d 29 in arrow function in jsdo arrow functions work without parameter in javascriptjs arrow functionsarrowfunction javascriptarrow function javascript mdnwhen should i use arrow functionses6 arrow functionarrow function javascript tutorialuse of arrow functions in javascriptjs arrow function as parameterarrow functionjs return from arrow functionjavascript access arrow function propertiesarrow functions in javascriptanonymous function arrow javascriptusing arrow function in javascriptarrow functioj jsjavascript arrow functionwhy we use arrow function in javascriptjs arrow 3ehow to make a callback arrow function javascriptwhy arrow functions are usedarrow functionsjavascript arrow function arguments objectarrow functions examplesfunction arrow javascriptjavascript es6 arrow functionarrow function example in javascript practicewhat 27s an arrow function in jstype of arrow function jsjavascript arrow function default parametersjavascript arrow function thisis better to use arrow function in classes jsarrow function syntax in javascriptarrow function js namedoes arrow functions take arguments javascript arguments arrow functionjs arrow function with parameterarrow function jswhat is the use of arrow functionsarrow function in javascript benefitsarrow functions thisdouble arrow functions in javascriptis arrow function es6 3fjavascript arrow function this keywordjs6 arrow functionarrow functions syntaxwhat is arrow function in javascriptarrow functions javascriptarrow function run time declaration javascripthow to pass arguments in arrow function javascriptadvantages of arrow functions in javascriptjs arrow function with paramshow to set up an arrow function in an object jsjavascript arrow function syntaxarrow function javascript bigarrow dunction javascriptarrow functions this keyword in jsjavascript what are arrow functions mdnarrow function in javascript w3schoolsarrow method in javascriptarrow functions basic usages in javascript