Javascript – What does the plus sign do in ‘+new Date’

javascriptsyntax

I've seen this in a few places

function fn() {
    return +new Date;
}

And I can see that it is returning a timestamp rather than a date object, but I can't find any documentation on what the plus sign is doing.

Can anyone explain?

Best Answer

that's the + unary operator, it's equivalent to:

function(){ return Number(new Date); }

see: http://xkr.us/articles/javascript/unary-add/

and in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus