Need to choose what happens when an event fires or how an array is sorted? In JavaScript, a function can be handled as a value: stored, passed to other code, or returned. It is an object with one extra capability—it can be invoked. This differs from the assumption that functions are only named pieces of code called directly. section 5.2.1 section 5.2.2 section 1.1
A function value can be:
- Assigned to a variable, an array entry, or an object property. section 5.2.1
- Passed as an argument or returned as a result. section 5.2.1
- Given properties of its own, created and assigned dynamically. section 5.2.1
Pass a callback, then call it
`function call(callback) { return callback(); }` `call(function () { return "ready"; });` The anonymous function is created where an expression can appear and passed to `call`. Inside `call`, the parameter refers to that function; `callback()` invokes it. Passing `callback` and writing `callback()` are different actions. Defining a one-use function at the call site also avoids giving it an unnecessary name elsewhere. section 5.2.1 section 5.2.2
const values = [0, 3, 2, 5, 7, 4, 8, 1]; values.sort((value1, value2) => value1 – value2);
An array’s `sort` call receives an arrow function as its comparator, supplying the comparison behavior without requiring you to implement the sorting algorithm. section 5.2.2
The same pattern supports event handlers: code registers a function, and the browser calls it when the event occurs. First-class functions also provide a starting point for composing functions, though that broader functional-programming style is a separate topic. The practical rule is simple: keep the function as a value when other code needs to store or call it; add `()` when you mean to invoke it now. section 5.2.2 section 5.2.1 section 5.6

