Table of contents
1.
Introduction
2.
What are First-Class Functions in Javascript?
3.
Usage of First-Class Function
4.
Assigning to Variable
5.
Passed as arguments to functions
6.
Returned as results of functions
7.
Included in any data structures
8.
Frequently Asked Questions
8.1.
Why do we need the First-class function?
8.2.
What is a First-class function in JavaScript medium?
8.3.
What is the difference between First-Class and Higher-Order Functions in JavaScript?
9.
Conclusion
Last Updated: Sep 5, 2024
Easy

First-Class Functions in Javascript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

First-class functions in JavaScript are functions that are treated as first-class citizens, allowing them to be assigned to variables, passed as arguments to other functions, and returned from functions. This feature is fundamental to JavaScript's flexibility and enables programming techniques such as higher-order functions, callbacks, and functional programming.

First-Class Functions in Javascript

Also Read About, Javascript hasOwnProperty

What are First-Class Functions in Javascript?

A programming language is considered First-class functions when the functions in that language are treated as java class objects like any other variable. They can also be parsed as arguments into different functions. For example, in such a language, assigned as a value to a variable, a function can be parsed as an argument to other functions and returned by another function. 

In JavaScript, functions are treated as another object type. Thus, first-class functions in Javascript supports following described operations:

  • Can be stored as a value in a variable
  • Can be returned by another function
  • It can be passed as a function's argument
  • It can be stored in any data structure.
  • It can have its properties and methods.

Usage of First-Class Function

  • Assign to Variables: Functions can be assigned to variables, allowing them to be used as any other value.
  • Pass as Arguments: Functions can be passed as arguments to other functions, enabling callbacks and event handling.
  • Return from Functions: Functions can be returned from other functions, allowing the creation of higher-order functions and closures.
  • Stored in Data Structures: Functions can be stored in arrays or objects, enabling dynamic and flexible function management.
  • Used in Function Composition: Functions can be composed to create more complex operations by chaining or nesting them.
     

Assigning to Variable

const foo = function() {
   console.log("foobar");
}
foo(); 
You can also try this code with Online Javascript Compiler
Run Code

Output

foobar
You can also try this code with Online Javascript Compiler
Run Code

 

Passed as arguments to functions

When functions can be passed like any parameter/arguments to a function, like:

const nums = [1, 2, 3, 4, 5]

const addone = (n) => n + 1

const addedOne = nums.map(addone)
You can also try this code with Online Javascript Compiler
Run Code

 

Output

2, 4, 5, 6
You can also try this code with Online Javascript Compiler
Run Code

 

We have an addone function that is treated as a variable and passed to the .map function. That's being said, the addone function is a first-class function.

Returned as results of functions

When functions can return another function, like:

function sayHello() {
   return function() {
      console.log("Hello Coders");
   }
}
You can also try this code with Online Javascript Compiler
Run Code

 

Output

Hello Coders
You can also try this code with Online Javascript Compiler
Run Code

 

In this example, We have returned a function from another function - We can return a function because we treated a function in JS as a value.

Note: A function is called a Higher-Order Function that returns a function.

 

Included in any data structures

When these functions can be stored in any data structure(stack, queue), like:

const wakeUp = name => `${name}, wake up early!`
const takeShower = name => `${name}, take shower!`
const workout = name => `${name}, workout!`
const shutUp = name => `${name}, shut up!`

const morningRoutines = [
    wakeUp,
    takeShower,
    workout,
    shutUp
]

morningRoutines.forEach(routine => routine('Harry'))
You can also try this code with Online Javascript Compiler
Run Code

 

Output

Harry, wake up early!
Harry, take a shower!
Harry, workout!
Harry, shut up!
You can also try this code with Online Javascript Compiler
Run Code

You can practice by yourself on an online javascript compiler.

Frequently Asked Questions

Why do we need the First-class function?

First-class functions enable flexible and powerful programming techniques in JavaScript. They allow functions to be passed around, stored, and manipulated just like any other value. This capability supports higher-order functions, callbacks, and functional programming, leading to more concise, modular, and reusable code.

What is a First-class function in JavaScript medium?

In JavaScript, a first-class function is a function treated like any other variable. It can be assigned to variables, passed as arguments, returned from other functions, and stored in data structures. This characteristic allows for dynamic and flexible programming, enabling patterns like callbacks and function composition.

What is the difference between First-Class and Higher-Order Functions in JavaScript?

  • A function is treated as a variable that can be assigned to any other variable or passed as an argument.
  • The function receives another function as an argument or returns First-order a new function.

Conclusion

In conclusion, first-class functions in JavaScript are a foundational concept that significantly enhances the language's flexibility and power. By treating functions as first-class citizens, JavaScript enables developers to write more modular, reusable, and expressive code. This capability is crucial for advanced programming techniques like higher-order functions, callbacks, and functional programming, making first-class functions essential for mastering JavaScript development.

Live masterclass