What are First-class Functions in JavaScript?

What are First-class Functions in JavaScript?

As per MDN definition,

JavaScript is a lightweight, interpreted, or Just In Time compiled programming language with First-Class Functions

In this post, let’s try to explore what are first-class functions and how it fits in JavaScript.

The programming language realm consists of many entities such as conditionals, types, loops, functions, etc., Each of them serving different needs for designing solutions, building applications. However, not all entities are the same. Options available for one entity might not be available for others.

First-class citizen is the term used to refer to entities that support all operations generally available to other entities. Some of these operations are assigning variables, modification, passing as an argument, returning from a function, etc.

Any programming language which treats functions as its First-class citizen is referred to have First-class functions.

In JavaScript, a function can be assigned to a variable, it can be passed as an argument to another function, can be returned as a result of another function making JavaScript a language with First-class functions.

First-class functions are the key feature enabling functional programming possible in JavaScript. Functional programming in which Higher-Order Functions is one of the standard practices is possible as the function can be passed an argument & returned as a value.

Please find some of the code samples below demonstrating their usage below:

Hope it helps you understand the First-class functions in JavaScript. Please share your thoughts.

Follow me for more interesting posts on JavaScript & Web Development. Also, please give a follow on Twitter.