Hero image

First Class Functions


In simple terms, First Class functions just means that functions are normal variables. They can do anything a normal variable can do, such as be added to objects, put in arrays or passed as parameters. The only thing special about them is that they can be invoked.

Syntax

The most common form of function as a variable these days is array functions, but they can be any sort of function.

JS
Typescript

Why is this useful?

Now that we know they are just like any other variable, we can start passing functions around and extending their functionality. What we can do is keep our functions really small, then build them up into bigger and better things as we go.

Passing as parameters

One of the best things about First Class functions is being able to pass through code to Pure functions, which lets you have dynamic code in something pure.

It is also really common in javascript to pass through a callback to an asynchronous function that will be run when required. This was more common before promises, but you will still see it about.

JS

There is not really anything more to them. Just treat them like a normal variable and you will have First Class functions.



What to read more? Check out more posts below!