Loading ...
Sorry, an error occurred while loading the content.

+new Date() - a question of readabilty

Expand Messages
  • walfisch_in_the_sea
    +new Date() === new Date().getTime(); +new Date() returns a timestamp. Is this good style? Or should it be avoided?
    Message 1 of 2 , Mar 6, 2010
    • 0 Attachment
      +new Date() === new Date().getTime();

      +new Date() returns a timestamp. Is this good style? Or should it be avoided?
    • Harry Whitfield
      ... new Date() returns an object. +new Date() forces conversion of the object to be a number as if you had used new Date().valueOf(). You could also do that in
      Message 2 of 2 , Mar 6, 2010
      • 0 Attachment
        On 6 Mar 2010, at 13:15:15, walfisch_in_the_sea wrote:

        > +new Date() === new Date().getTime();
        > +new Date() returns a timestamp. Is this good style? Or should it be avoided?

        new Date() returns an object.

        +new Date() forces conversion of the object to be a number as if you had used new Date().valueOf().

        You could also do that in other ways:

        1*new Date()

        Number(new Date())

        etc.

        None of this tricky coding is as clear as using new Date().getTime(), so imo the tricky coding is not good style and should be avoided. Program text should be written in a way that is clearest for people to read and understand and not so as to save typing a few extra characters.

        Cheers,

        Harry.
      Your message has been successfully submitted and would be delivered to recipients shortly.