Functions are Data
This is an important concept that we'll need later on—functions in JavaScript are actually data. This means that the following two ways to define a function are exactly the same:
function f(){return 1;} var f = function(){return 1;}
The second way of defining a function is known as function literal notation.
When you use the typeof
operator on a variable that holds a function value, it returns the string "function".
>>> function f(){return 1;} >>> typeof f
"function"
So JavaScript functions are data, but a special kind of data with two important features:
- They contain code
- They are executable (can be invoked)
As you saw before, the way to execute a function is by adding parentheses after its name. As the next example demonstrates, ...
Get Object-Oriented JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.