First-Class Functions and Function Expressions in JavaScript
October 10, 2015
In JavaScript functions are first-class. This mean that with functions you can do everything you can do with other types such as assigning them to variables.
Wikipeida on first-class functions:
In JavaScript functions are objects. So when you create a function you are actually creating an object. This object will have certain properties one of which will be the function. Lets take a look at this.
Lets first make a simple function statement. You've all seen function statements, it's one of the first things you will learn in JavaScript. Functions Statements don't result in a value until they are exectued. Function statements are also known as function declarations so if you see this don't get confused.
If you ran this nothing would happen because it's not been invoked. Remeber function statments don't return a value until they are exectuted. If we were to invoke this we would be creating an object which has some properties one of which will be the 'hello' function. So rather than calling the hello function, which will just console.log 'Howdie', lets log the function itself.
If you run this in a browser you will see the function itself being returned. Now remeber that functions are objects which have properties. So lets see one of those properties.
function.name is a now part of the ECMAScript 2015 (ES6) standard. But it has been around in a JavaScript a while. This name value is always equivalent to the identifier that follows the function keyword.
Lets add another property
To really prove that functions are objects lets use Node.Js to show the object itself. Change the file to just console.log(hello). Run your file using
What you will now see in the console is the object itself
So again we see that functions are object that have properties. One of those properties being the function itself which you can invoke. This is very powerful because it means functions are first-class and we can do lots of cool things with them. Lets take a look at some of the ways functions are used in JavaScript. You've already seen function statements, these are pulled into memory when it's execution context is created. This means that they can be called before they're created. This is known as hoisting or more techinally function declaration hoisting.
Or as Nicholas Zakas put it in his fantastic book:
You've most likely seen function expressions as well. An expression is a unit of code that returns a value.
A function expression is a block of code that results in a value.
Here a function is created and assigned to the variable logNum. Remeber that functions can do anything other types in JavaScript can do. Now this function is considered an anonymous function because it has no name. So if you were to
You would get an empty string back - it's anonymous. When you run a JavaScript program all variables are first set to undefined. Only once they are assigned in the program are they assigned a value.
This will cause an error
This is because when JavaScript runs it is run line by line, so when the program gets to logNum(); it is undefined. Only once we assign the logNum variable to the function does it point to the function.
Finally lets pass a function as a variable.
Here the variable func is going to be a function. So the parameter we pass to logText will be a function itself, we can do this because functions are first-class. We then invoke the function within the logText function. So lets give this a go.
And I'm sure you know what we are going to get back here.
So lets summarise first-class functions:
Just like other variables in JavaScript functions
are an instance of the Object type
have propeties and a link back to its constructor method
can be stored in a variable
can be passed as a parameter to another function