Quick Overview
Passing `fn` hands over the function; `fn()` hands over its result. That one pair of parentheses is the difference between scheduling work for later and having already run it.
In JavaScript, functions are First-Class Citizens. This means functions can be treated like any other value: stored in variables, passed as arguments, and returned from other functions.
A Higher-Order Function is a function that receives another function as an argument (a Callback) or returns a function. Higher-order functions enable asynchronous programming and custom utilities.
function processUser(name, callback) {
const formattedName = name.trim().toUpperCase();
callback(formattedName);
}
// Passing an inline callback function
processUser(" alex ", function(result) {
console.log(`Processed User: ${result}`); // "Processed User: ALEX"
});
Callbacks allow code to run after an async operation completes (such as timer events or network data loads):
console.log("Start");
setTimeout(() => {
console.log("2 Seconds Elapsed");
}, 2000);
console.log("End");
processUser("Alex", callback()) executes callback immediately instead of passing the function reference. Omit parentheses when passing callback function references: processUser("Alex", callback)!Write it yourself. The tests below run against your code.
Callbacks are the building blocks of async operations. Next, let's master the this keyword and function binding!
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.