Arrow function (=>)

Creates a concise function expression that lexically binds this.

Since ES2015 (ES6) Spec ↗

Syntax

(params) => expression
(params) => { statements }

Returns

function — A new function object.

Examples

const add = (a, b) => a + b;
console.log(add(2, 3));
Output
5
const nums = [1, 2, 3];
console.log(nums.map(n => n * n));
Output
[ 1, 4, 9 ]
const makeCounter = () => {
  let n = 0;
  return () => ++n;
};
const c = makeCounter();
console.log(c(), c());
Output
1 2

Notes

- Arrow functions do not have their own `this`, `arguments`, `super`, or `new.target`; they inherit `this` lexically. - They cannot be used as constructors (`new` throws). - To return an object literal concisely, wrap it in parentheses: `() => ({ a: 1 })`.

Browser & runtime support

EnvironmentSince version
chrome 45
firefox 22
safari 10
edge 12
node 4.0

See also