Quick Overview
An arrow function has no `this` of its own and borrows the surrounding scope's. That is exactly what makes it right for callbacks and wrong for object methods, where `this` should be the object.
ES6 introduced arrow functions (() => {}), offering a concise syntax for writing function expressions.
Arrow functions reduce boilerplate code, make functional array methods (.map(), .filter()) elegant, and lexically bind the this keyword.
If an arrow function body consists of a single expression, you can omit the curly braces {} and the return keyword, causing the expression to evaluate and return implicitly:
// Traditional Function Expression
const squareTraditional = function(x) {
return x * x;
};
// Arrow Function with Explicit Return
const squareExplicit = (x) => {
return x * x;
};
// Arrow Function with Implicit Return (Concise 1-liner!)
const squareImplicit = (x) => x * x;
console.log(squareImplicit(5)); // 25
() => console.log("Hello").x => x * 2.(a, b) => a + b.({ key: value }). Otherwise, JavaScript misinterprets curly braces as a function block!// WRONG: Returns undefined because braces are interpreted as a block!
const makeUserWrong = (name) => { username: name };
// CORRECT: Parentheses tell JS this is an object literal!
const makeUserCorrect = (name) => ({ username: name });
console.log(makeUserCorrect("Alex")); // { username: "Alex" }
Write it yourself. The tests below run against your code.
Use arrow functions for clean callbacks and utility expressions. Next, let's understand how the Scope Chain searches for variables!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.