Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is First-Class Function in JS
2.1.
Assigning to Variable
2.2.
Passed as arguments to functions
2.3.
Returned as results of functions
2.4.
Included in any data structures
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

First-Class Functions

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

 

Introduction

JavaScript is the most popular dynamic scripting language on the web. One of the features of this language is the First-class function.

In this write-up, we will discuss the first-class function, what it is, what the use is, and will also look at some of its practical implementations to understand its usage and work more explicitly.

 

Don't worry if you are new to the language or have studied it long before. I will provide you with the link to the content regarding the basics of Javascript language so that you can practice it as much as possible.

Also Read About, Javascript hasOwnProperty

What is First-Class Function in JS

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, JS first-class functions support the 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.

 

We will see the implementation of these operations and will try to understand the usage of the first-class method in a better way.

 

Assigning to Variable

 

const foo = function() {
   console.log("foobar");
}
foo(); 

 

Output

foobar

 

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)

 

Output

 

2, 4, 5, 6

 

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");
   }
}

 

Output

 

Hello Coders

 

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'))

 

Output

Harry, wake up early!
Harry, take a shower!
Harry, workout!
Harry, shut up!

You can practice by yourself on an online javascript compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

1. What is a Js First-class function?

  • The first-class functions are treated as the first-class object which can be stored, passed as an argument in functions, variables, returned from other functions, have their properties. 

 

2. 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.

 

Key Takeaways

In this article, we have extensively shown what is JS's first-class function and its implementations in different ways.

A Programming language is said to have First-Class functions when it has no restrictions on how functions can be created or used. The first-class function is just a feature that would allow treating functions quickly as variables and moved around without restrictions. We can build powerful utilities with that feature, like higher-order functions, closures, currying, etc.

Check out this link if you want to explore more about javascript.

You can also know more about the Difference between Argument and Parameter.

If you are preparing for the upcoming Campus Placements, don't worry. Coding Ninjas has your back. Visit this data structure link for cracking the best product companies.

Happy Learning!!!

Previous article
Higher-Order Functions in JS
Next article
Unary Functions and Pure Function
Live masterclass