Skip to content

Latest commit

 

History

History
45 lines (32 loc) · 732 Bytes

firstClassCitizen.md

File metadata and controls

45 lines (32 loc) · 732 Bytes

First-class citizen

A first-class function is a function that can be treated like any other variable.

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

Passing function as value

const square = function(x){
  return x * x;
}

square(5); //25

Passing function as argument

function sayHello(){
  return "Hello ";
}

function greeting(message, name){
  console.log(message() + name);
}

greeting(sayHello, "World");

Returning a function

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

const foo = sayHello();
foo() // hello;
// or
foo()();