First Class Functions

In this part, we will discuss a topic that I think distinguishes those who only use JavaScript, and those who understand how JavaScript under the hood works. This idea is called _First Class Function_s. In JavaScript Functions ARE Objects.

First Class Functions mean that in a programming language (JavaScript is not the only one, but certainly the most popular one), all that we can do with other types, objects, strings etc can be done with a function. You can assign them to variables, pass them around, create them on the fly.

Many people who have previously had contact with programming in a different language, are suprised that you can attach to a function an object, a function or a primitive. This is what we get thanks to a functions being objects.

In JavaScript, functions have hidden special properties and two quite important

  1. Name, in the JavaScript function does not have to have a name, it can be anonymous.
  2. Code, code in the function object is really a property.

So what does it look like?

function hello() {
  console.log("Hello world!");

hello.firstName = "John";


Note that in the fifth line, we did something that we could not in many other languages. We added a property to the function.

This is not a mistake. To prove it, just look at what we received thanks to console.log.

Function Expressions / Function Statements


Is a unit of code with a value that has to be saved in a variable.

The function below is an anonymous function (a function without a name).

Functions stored in variables do not need function names. They are always invoked (called) using the variable name.

var myVar = functio() {
 console.log("Function Expression"); 


The function statement declares a function.

A declared function is "saved for later use", and will be executed later, when it is invoked (called)

Function Statements are hoisted into memory.

function hello() {
 console.log("Function Statement"); 

Next Post

I will talk about two concepts. By value and by reference.