thisIntroduced in ES6, arrow functions provide a concise syntax for writing function expressions. Arrow functions do NOT have their own this, arguments, super, or new.target bindings.
flowchart TD
FuncType["Function Creation"] --> Std["Standard Function (function() {})"]
FuncType --> Arrow["Arrow Function (() => {})"]
Std --> S1["Dynamic 'this' binding at runtime"]
Std --> S2["Has 'arguments' pseudo-array"]
Std --> S3["Can be instantiated with 'new'"]
Arrow --> A1["Lexical 'this' (Inherited from outer parent scope)"]
Arrow --> A2["No 'arguments' object (Use rest ...args)"]
Arrow --> A3["Cannot be used as Constructor (Throws TypeError)"]
| Feature | Standard Function | Arrow Function |
|---|---|---|
| Syntax Length | function(a, b) { return a + b; } |
(a, b) => a + b (Implicit return) |
this Binding |
Dynamic based on invocation site | Lexical (Static parent scope binding) |
| Constructor Usage | new FunctionName() allowed |
Forbidden (Throws TypeError) |
arguments Object |
Built-in arguments array-like |
Absent (Use ...args rest parameter) |
// Demonstrating Arrow function syntax variants, lexical this, and implicit returns
// 1. Syntax Variants & Implicit Return
const add = (a, b) => a + b; // Single expression implicit return
const square = x => x * x; // Single parameter parenthesis omission
console.log(`Add: ${add(5, 10)}, Square: ${square(4)}`);
// Returning Object Literals Implicity (Wrap in parentheses!)
const createUser = (id, name) => ({ id, name, active: true });
console.log("Implicit Object Return:", createUser(1, "Alex"));
// 2. Lexical 'this' in Callbacks
function Counter() {
this.count = 0;
// Arrow function retains 'this' reference to Counter instance
setInterval(() => {
this.count++;
if (this.count <= 2) {
console.log(`Counter tick: ${this.count}`);
}
}, 100);
}
new Counter();
// 3. Absence of arguments Object (Use Rest Parameters)
const sumAll = (...args) => args.reduce((sum, n) => sum + n, 0);
console.log(`Sum All: ${sumAll(10, 20, 30)}`);
() => ({ key: "val" }) so the parser does not mistake curly braces for a function block.arr.map(x => x * 2).this.Why does const getObj = () => { a: 1 }; return undefined instead of an object?
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.