views:

191

answers:

3

Looking through the jQuery source in the function now() I see the following:

function now(){
    return +new Date;
}

I've never seen the plus operator prepended to the new operator like this. What does it do?

+10  A: 

I think the unary plus operator applied to anything would cause it to be converted into a number.

Nicolás
Hm, does + always convert the expression into a number? + is a valid operator for both strings and numbers. 1* is usually used to force an argument into a number.
Ernelli
@Ernelli, + is only a valid string operator when it's used as a binary operator. The unary + operator only applies to numbers, so it converts the same way that 1* does.
Matthew Crumley
Ernelli: See CMS's answer below http://stackoverflow.com/questions/1983040/what-does-the-new-mean-in-javascript/1983109#1983109. Unary `+` seems to always convert its operand to a number, while binary `+` can be used on strings.
Brian Campbell
+9  A: 

It converts the Date() into an integer, giving you the current number of milliseconds since January 1, 1970.

Brian Campbell
5 seconds too late, I've voted for Nicolas ;-)
Michael Krelin - hacker
My browser crashed while writing this! I had to launch a new browser...
Brian Campbell
@Brian IE will do that from time to time. ;)
Darrell Brogdon
Actually, it's a WebKit nightly build... I suppose I should expect that from a nightly build.
Brian Campbell
Good excuse ;-) I wonder who upvoted your comment, someone must have seen you relaunching the browser! ;-)
Michael Krelin - hacker
That's kind of creepy... who is watching me post on Stack Overflow? What are they doing in my house? o_O
Brian Campbell
+17  A: 

Nicolás and Brian are right, but if you're curious about how it works, +new Date(); is equivalent to (new Date()).valueOf();, because the unary + operator gets the value of its operand expression, and then converts it ToNumber.

You could add a valueOf method on any object and use the unary + operator to return a numeric representation of your object, e.g.:

var productX = {
  valueOf : function () {
    return 500; // some "meaningful" number
  }
};

var cost = +productX; // 500
CMS
+1, good explanation.
musicfreak
Since `valueOf()` is returning a number isn't prepending the unary + operator basically unnecessary in the above example? I would think it would be more meaningful if valueOf() returned `"500"`.
Darrell Brogdon
@Darrel, are you looking at different code example than I am? I don't see any edits to this post, so your comment seems strangely out of touch and irrelevant, but almost like the post could have been edited after the comment.
Breton
@Breton The "Since valueOf() is..." question is in response to @CMS's answer to my original question.
Darrell Brogdon
@Darrell: Well, it depends on the semantics of how do you will use your object, e.g. if you return a `String`, the unary `+` operator will work as expected (because internally it invokes `ToNumber`), but if you use the binary `+` operator, on two objects that its `valueOf` return a `String`, a String concatenation will be made, instead of a sum...
CMS