DEV Community

Cover image for First Class Functions
Mugendi Njue
Mugendi Njue

Posted on

First Class Functions

Today i learned that JavaScript is a lightweight, interpreted,or JIT(Just In Time) compiled language with first class functions.
What are first class functions? Well a programming language is said to have first class functions when functions in that programming language are treated like any other variable.Example

// Assigning a variable to a function
const foo = () => {
  console.log("FooBar");
}
// Invoking the function
foo();
Enter fullscreen mode Exit fullscreen mode

You can also have a function that returns another function, for example

const sayHello = () => {
   return () => {
      console.log("Hello!");
   }
}
// Method 1 of function invoking
const myFunc = sayHello();
myFunc();
// Method 2 of function invoking
sayHello()();
Enter fullscreen mode Exit fullscreen mode

Functions that return other functions are called Higher-Order Functions.

Top comments (0)