0
+ + new Date

Output

1429943200207

I saw this strange statement in our thirdparty plugin. I tried to break the logic but I can't. It returns the current time in ms as same as new Date().getTime().

Is that an alternate way to return in current time ms?

How it works?

2
  • What's on the line before it? Commented Apr 25, 2015 at 6:33
  • @kojow7 return + + new Date + Math.random(); Commented Apr 25, 2015 at 6:34

1 Answer 1

3

When you put unary + before a value, it is converted to a number. So

+ new Date

is equivalent to

Number(new Date)

And converting a Date to a number returns the time in milliseconds, so they're both equivalent to

(new Date).getTime()

I'm not sure why you have two +, though. Maybe the first one is part of a larger expression, like

"The timestamp is " + + new Date

Then the first + is for concatenation.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.