DEV Community

Discussion on: 5 Ways to Write Functions in JavaScript

Collapse
 
jcubic profile image
Jakub T. Jankiewicz

There is also Function constructor:

var sum = new Function('a,b', 'return a + b');
Enter fullscreen mode Exit fullscreen mode

Note that Functions are instances of Function.

(function() {}) instanceof Function;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
koladev profile image
Mangabo Kolawole

Great Jakub.

I wanted to add this one in the article, but it's not really widely used.

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

You're right, but to be complete you need to know about it.

Also, you missed named function expressions:

const mul = function recur(a, b){
    if (a === 0) {
       return b;
    }
    return recur(a -1, b + 1);
};
Enter fullscreen mode Exit fullscreen mode

and immedietly invoked function expressions:

(function() {
   const x = 10;
   console.log({x});
})();
Enter fullscreen mode Exit fullscreen mode