In JS, we should consider “function” to take the broader meaning of another related term: “procedure.” A procedure is a collection of statements that can be invoked one or more times, may be provided some inputs, and may give back one or more outputs.

    From the early days of JS, function definition looked like:

    This is called a function declaration because it appears as a statement by itself, not as an expression in another statement. The association between the identifier and the function value happens during the compile phase of the code, before that code is executed.

    This function is an expression that is assigned to the variable awesomeFunction. Different from the function declaration form, a function expression is not associated with its identifier until that statement during runtime.

    It’s extremely important to note that in JS, functions are values that can be assigned (as shown in this snippet) and passed around. In fact, JS functions are a special type of the object value type. Not all languages treat functions as values, but it’s essential for a language to support the functional programming pattern, as JS does.

    JS functions can receive parameter input:

    Functions also can return values using the keyword:

    You can only return a single value, but if you have more values to return, you can wrap them up into a single object/array.

    Since functions are values, they can be assigned as properties on objects:

    There are many varied forms that functions take in JS. We dig into these variations in Appendix A, “So Many Function Forms.”