user10173262
user10173262

Reputation: 9

Why we enclose value of variable in parenthesis?

I have seen multiple nodejs examples where

var minute = (new Date() ). getMinutes();

is defined like that. Why is this date object in parenthesis when

var minute = new Date().getMinutes();

works

Upvotes: 0

Views: 209

Answers (2)

Eriks Klotins
Eriks Klotins

Reputation: 4180

Lets test it:

// let there be
function someclass()
{
    console.log("constructor"); 
    this.method = function()
    {
       console.log("method");
    }
}

// Trying to call different versions

new test().method()
// Output:
// constructor
// method

(new test()).method()
// Output:
// constructor
// method


// just to be sure:
new Date().getTime() === (new Date()).getTime()
// Output:
true

As shown here and mentioned by others, there is no semantic need for the parentheses.

Upvotes: 0

andydavies
andydavies

Reputation: 3313

The new keyword could be ambiguous (to a human) without the parentheses. i.e. is it a new Date() or is it a new Date().getMinutes().

Upvotes: 1

Related Questions