Functions are Objects
The typeof
operator declares functions as a type.
function add (x, y) { return x + y; } console.log(typeof add)
However, functions are in fact objects (well, they are special Function objects that you can invoke). That is where the "functions are values" comes from. The fact that functions are objects enable you to treat them like other values (store them in variables, pass them to functions, etc).
You can go so far as to create a function using an object constructor notation!
const multiply = new Function("x", "y", "return x * y;"); console.log(typeof multiply); console.log(multiply(2, 3));
Functions, like other built-in objects, have instance properties and methods!
function add (x, y) { return x + y; } console.log(add.name); console.log(add.length); // number of parameters console.log(add.toString());