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