-1

In this code:

const express = require('express');
const app = express();

we are calling a function (or a class since classes are function in JS) from another module called express. I just want to know why sometimes we use keyword new and what is the difference.

What is the difference between the above code and this one:

const express = require('express');
const app = new express();
Asker
  • 1
  • Very strongly related: [What is the 'new' keyword in JavaScript?](https://stackoverflow.com/q/1646698) – VLAZ May 28 '22 at 15:36
  • 1
    `new` creates the object, calls the constructor function to fill in its values, then returns the object. Without `new` the function has to create the object and return it itself. – Barmar May 28 '22 at 16:26
  • @Barmar Could you please elaborate on that? What would be the difference in my example? – Asker May 28 '22 at 20:40
  • `new express()` creates a new object and initializes it using the `express()` function. `express()` simply calls the `express()` function. Did you read the duplicate question? – Barmar May 28 '22 at 20:41

0 Answers0