JavaScript may look familiar if you know C# or Java, but its functions deserve a different mental model: a function is an object you can move around, not just a named block of code. “First-class” means it can be handled like other values. It also has one extra capability: you can invoke it. section 1.1 section 3.1.1
A function can be:
- Created with a literal and assigned to a variable, an array entry, or an object property. section 3.1.1
- Passed to another function as an argument, or returned from a function. The receiving function can invoke a passed-in function later as a callback. section 3.1.1
- Given dynamically assigned properties, just like other objects. section 3.1.1
Define it where you need it
Compare `var a = 3; myFunction(4);` with `var a = function() {}; myFunction(function(){});`. A function expression can occupy the same assignment or argument position as another value. Unlike a function declaration, it is part of another statement; placing it at the point of use can make code easier to follow. section 3.3.1
Listing 3.5. Function declarations and function expressions
function myFunctionDeclaration(){ #A
function innerFunction() {} #B
}
var myFunc = function(){}; #C
myFunc(function(){ #D
return function(){}; #E
});
(function namedFunctionExpression () { #F
})(); #F
+function(){}(); #G
-function(){}(); #G
!function(){}(); #G
~function(){}(); #G
Compare the standalone and nested declarations with expressions assigned, passed as arguments, and returned as values. section 3.3.1
This flexibility supports callbacks and is a starting point for functional programming: composing functions to solve problems. That broader style, along with closure and scope mechanics, is a separate topic. section 3.1.1 section 1.1






