Difference between var a = foo() & var a= new foo()

Lets suppose I have a constructor function:

function foo() {
console.log("manjula");
}

Code:

var a = foo();
//a will be undefined 
Output will be manjula

The above Executes the function foo() and variable a will always hold a value that is returned from the function foo

var a = new foo();
//a will be foo {}
Output will be manjula

Creates an object using function a() as a constructor and will assign it to variable a. Since the output will be same as above. In the above code It calls the function foo and outputs “manjula” with ‘this’ being pointed to the new object ‘a’ that we just created above.

Like what you read? Give Manjula Dube a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.