JavaScript treats functions as a first class citizens. It means that the language treats functions as values.

  • Assigning a function to a variable
  • Storing a function in a data structure (object).
  • Passing a function as an arguments to other another function (high-order function)
  • Returning them as a value from another function (high-order function)

Function Statement aka Function Declaration

function fn() { }

Function Expression

const fn = function () { };

Arrow Function Expression aka Lambda Expression

const fn = () => { };

It is important to know the most common limitations of an Arrow Function Expression:

  • (common) will not bind "this" object to the current function
  • (common) will not bind "arguments" object to the current function
  • can not be used as a constructor
  • can not be used as a generator function (yield keyword)
  • (rare) no access to the new.target

Does NOT work or compile

const fn = function namedFn() => { };
namedFn(); // ReferenceError: namedFn is not defined

const arrowFn = namedArrowFn() => { }; // SyntaxError: Malformed arrow function parameter list

Immediately Invoked Function Expression (IIFE)

(() => { });
// OR
(function () { })();
// OR
(function () { }());

Also known as Self-Executing Anonymous Function.

Difference between First Class Function and High-Order Function

  • "has first-class functions" is a property of a language (functions are treated like any other variable)
  • "is higher-order" is a property of a function itself (takes one or more functions as arguments OR returns a function as its result)

Difference between parameters and arguments

  • Arguments are the values (1, 2) itself -- fn(1, 2)
  • Parameters are the variables (param1, param2) itself -- function fn(param1, param2) { }