Functional JavaScript: First-Class Citizens
In JavaScript, functions are first-class citizens. This means you can treat functions like any other object: assign them to variables, pass them as arguments, and return them from other functions!
javascript
Modern Functions: Arrow Functions (ES6)
Arrow functions are a concise way to write functions. Beyond being shorter, they have one major difference: they do NOT have their own
this keyword.javascript
Wait! Use arrow functions for almost everything, especially callbacks in objects or arrays. Only use traditionalfunctiondeclarations when you need thethiscontext (like in certain class methods or event handlers).
Advanced Parameters (ES6)
1. Default Parameters
Assign default values to arguments in case they are missing.
javascript
2. Rest Parameters (...args)
The rest parameter allows a function to collect an indefinite number of arguments as an array.
javascript
Closures: Functions that Remember
A closure is a function that "remembers" the variables from its outer (parent) function, even after the parent has finished running.
javascript
Higher-Order Functions
A function that takes another function as an argument (a callback) is called a higher-order function. These are incredibly common for handling events and manipulating arrays.
javascript
Summary
| Type | Syntax | Purpose |
|---|---|---|
| Declaration | function f() {} | Standard global function |
| Expression | const f = function() {} | Function as a variable |
| Arrow (ES6) | (x) => x * 2 | Concise, non-binding this |
| Default | (x = 10) => {} | Optional arguments |
| Rest | (...args) => {} | Indefinite arguments |
| IIFE | (() => {})() | Immediately-invoked function |
| Callback | f(callback) | Higher-order logic |
| Closure | f() { return f(); } | Functions with private state |
| Best For | Reusable logic, events, callbacks | |
| Parameter | Pass variables to functions | |
| Argument | The actual data passed! | |
| Pure Func | Returns same output for same input | |
| Side Effect | Function that modifies global state | |
| Recursion | Function that calls itself | |
| Hoisting | Function declarations are moved to the top of code! |