Arrow Functions

Anonymous Functions and Arrow Functions

A function expression creates a function as a value, often unnamed, to assign, pass as a callback or invoke at once as an IIFE (Immediately Invoked Function Expression). Arrow functions (ES2015) are shorter: (x) => x * x returns its expression. They have no own this, arguments, super or new.target (The Function Object), cannot be called with new and cannot be generators. A braced body needs return, and an object literal needs parentheses.

Function expressions, arrow functions and an IIFEJavaScriptLive
const square = function (x) { return x * x; };     // function expression
const cube = (x) => x * x * x;                     // arrow: implicit return
const noBraces = (x) => { x };                     // block body: returns undefined
const makePoint = (x, y) => ({ x, y });            // wrap an object literal in ( )
(function (msg) { console.log("IIFE", msg); })("runs at once");
console.log(square(3), cube(3), noBraces(3), makePoint(1, 2), square.name, cube.name);
try { new cube(); } catch (e) { console.log(e.message); }

Anonymous functions still get a name inferred from their variable, which keeps stack traces readable.