0

To create a new empty object in javascript we can write in 2 ways.
1. With object literal syntax:

const obj = {}

2.Or with constructor function syntax:

const obj = new Object()

Today I accidentally typed out const obj = new Object saw it also worked, where it should've thrown an error because I'd not invoked the constructor function which is done by a set of parenthesis.

I know that the new operator with a constructor function does 3 things.
1. creates a new empty object.
2. sets the value of this to the new object.
3. return the newly created object from the constructor function.


Is the constructor functions invocation is optional?
So what am I missing?

Sharandeep
  • 25
  • 5

1 Answers1

0

As a special case, for the new operator only, JavaScript simplifies the grammar by allowing the parenthesis to be omitted if there are no arguments in the function call. Here are some examples using the new operator:

o = new Object;  // Optional parenthesis omitted here
d = new Date();  

Originally answered here.

TheMisir
  • 4,083
  • 1
  • 27
  • 37