eYe
eYe

Reputation: 1733

What does the empty parentheses mean after 'require' declaration in Node.js?

In Node.js I see sometimes a declaration like this:

var App = require('express')();

What do the empty brackets '()' at the end mean?

I am suspecting the declaration above is equivalent to something like:

var Express = require('express');
var App = Express();

Is that right?

Upvotes: 2

Views: 1463

Answers (2)

Ales Maticic
Ales Maticic

Reputation: 1965

As James already answered the module returns a function which is than invoked in this manner.

Here a simple code sample to make it easier understandable.

function a() {
    function b() {
        alert('Alert me!');
    }
    return b;
}
a()();
//alerts 'Alert me!'  

Upvotes: 8

James
James

Reputation: 5169

Essentially the express module is returning a function. The empty brackets call the function so now App is the result of the returned function.

Upvotes: 3

Related Questions