Function

as First-class Citizen

QUIZ

1r.nu/eventloop

First-class citizen

 

In programming language design, a first-class citizen (also object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as a parameter, returned from a function, and assigned to a variable.

In JavaScript, functions are first-class objects,

i.e. they are objects and can be manipulated and passed around just like any other object.

Specifically, they are Function objects.

Functions can

  • be stored in variable
  • have properties
  • be properties of Objects
  • be arguments to other functions
  • be returned by other functions

Assigning functions to a variable

function multiply(x, y) {
   return x * y;
}
var multiply = function(x, y) {
   return x * y;
};
var multiply = function func_name(x, y) {
   return x * y;
};

Function declaration

Function expression

Named function expression

var multiply = new Function("x","y","return x*y")

Function constructor

And that’s why functions are first class citizens in JavaScript :)

Function as First-class Citizen

By Balázs Suhajda

Function as First-class Citizen

  • 1,041