JavaScript
  • JavaScript Introduction
  • JS Engine
  • V8 Engine
  • First-class function
  • Optimized Code
  • Call Stack & Memory heap
  • Single Thread
  • JavaScript RunTime
  • Nodejs
  • Context and Environment
  • Hoisting
  • Functions
  • Arguments
  • Variables
  • Scope
  • IIFE
  • this
  • call(), apply() and bind()
  • currying
  • Types
  • Type Coercion
  • Functions as Object
  • HOF (Higher Order Function)
  • Two pillars of Javascript
  • Closures
  • Prototypal Inheritance
  • OOP and FP
  • OOP
    • 4 principles of OOP
  • FP
    • Pure function
    • Imperative vs Declarative
    • Immutability
    • HOF and Closures
    • Currying
    • Partial Application
    • Compose and Pipe
  • Composition vs Inheritance
  • OOP vs FP
  • JS working
  • Promises
  • Async Await
  • ES5 - ECMAScript 2009
  • ES6 - ECMAScript 2015
  • ES7 - ECMAScript 2016
  • ES8 - ECMAScript 2017
  • ES9 - ECMAScript 2018
  • ES10 - ECMAScript 2019
  • ES11 - ECMAScript 2020
  • ES12 - ECMAScript 2021
  • JOB Queue
  • Promises Execution
Powered by GitBook
On this page
  • Assign a function to a variable:
  • Pass a function as an Argument:
  • Return a function:

Was this helpful?

First-class function

A programming language is said to have first-class functions if it treats functions as first-class citizens.

A function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

Assign a function to a variable:

const foo = function() {
   console.log("foobar");
}
// Invoke it using the variable
foo();

We assigned an Anonymous Function in a Variable, then we used that variable to invoke the function by adding parentheses () at the end.

Pass a function as an Argument:

function sayHello() {
   return "Hello, ";
}
function greeting(helloMessage, name) {
  console.log(helloMessage() + name);
}
// Pass `sayHello` as an argument to `greeting` function
greeting(sayHello, "JavaScript!");

We are passing our sayHello() function as an argument to the greeting() function, this explains how we are treating the function as a value.

Return a function:

function sayHello() {
   return function() {
      console.log("Hello!");
   }
}

We need to return a function from another function - We can return a function because we treated function in JavaScript as a value.

PreviousV8 EngineNextOptimized Code

Last updated 5 years ago

Was this helpful?