JS - which is correct, new date or new date ()?

What's the correct syntax in JavaScript:

var x = new Date;

      

OR

var x = new Date();

      

?

+3


source to share


3 answers


Both are correct, just a matter of taste.

But it's generally a good idea to use curly braces even if you don't pass any parameters, because these two snippets don't have the same score:

Incorrect



new Date.valueOf(); // to work it should be (new Date).valueOf()

      

Fix one

new Date().valueOf();

      

+3


source


Both are correct. The parentheses are optional when using an operator new

and there are no arguments.



+3


source


Both are correct. You can also use parameterized constructors.

EG: - var d = new Date(1993, 6, 28, 14, 39, 7);

    console.log(d.toString());     // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
    console.log(d.toDateString()); // prints Wed Jul 28 1993

      

-1


source







All Articles