DEV Community

 Bishnu Prasad Chowdhury
Bishnu Prasad Chowdhury

Posted on • Updated on

Javascript : types of functions

Function statements

// Allows hoisting

function test(){
   console.log("function statements");
};
test();
Enter fullscreen mode Exit fullscreen mode

Function expressions

// Doesnot allow hoisting

var a = function (){
   console.log(function expression);
};
a();
Enter fullscreen mode Exit fullscreen mode

Named function expressions

var a = function test(){
   console.log("named function expressions");
};
a();
Enter fullscreen mode Exit fullscreen mode

Function declaration

function test() {
   console.log("function declaration");
};
test();
Enter fullscreen mode Exit fullscreen mode

Anonymous function

var x = (){
};
x();
Enter fullscreen mode Exit fullscreen mode

First class function

// Ability to pass as argument and return functions as value

var x = function test(abc){
  console.log(abc);
  return function a(){
  };
};
x(function () {};);
Enter fullscreen mode Exit fullscreen mode

Arrow functions

var x = () => console.log("Arrow functions");
Enter fullscreen mode Exit fullscreen mode

Generator functions

function* square(a){
   for(let x=a;; x*=a){ 
      yield x;
      }
};

for(let z of square(2)){
    console.log(z);
}
Enter fullscreen mode Exit fullscreen mode

new Function

// Allows to convert string to function useful to handle server return codes.

var a = new Function ('a','b', 'return a+b');
console.log( sum(3,6));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)