higher and first order function Flashcards

(2 cards)

1
Q

first order function

A

A first-order function is a function that doesn’t accept another function as an argument and doesn’t return a function as its return value. i.e, It’s a regular function that works with primitive or non-function values.

const firstOrder = () => console.log(“I am a first order function!”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

higher order function

A

A higher-order function is a function that either accepts another function as an argument, returns a function as its result, or both. This concept is a core part of JavaScript’s functional programming capabilities and is widely used for creating modular, reusable, and expressive code.

The syntactic structure of higher order function will be explained with an example as follows,

// First-order function (does not accept or return another function)
const firstOrderFunc = () =>
console.log(“Hello, I am a first-order function”);

// Higher-order function (accepts a function as an argument)
const higherOrder = (callback) => callback();

// Passing the first-order function to the higher-order function
higherOrder(firstOrderFunc);
In this example:

firstOrderFunc is a regular (first-order) function.

higherOrder is a higher-order function because it takes another function as an argument.

firstOrderFunc is also called a callback function because it is passed to and executed by another function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly