2

I'm tinkering with some code found here: http://www.benknowscode.com/2012/11/selecting-ranges-jquery-ui-datepicker.html

It's all pretty clear except for one line-- in the code two vars are set to -1. Then they are used like this:

var cur = -1, prv = -1;
prv = +cur; // what does this do?
console.log(prv); // results -1
console.log(cur); // results -1

I'm familiar with the += usage-- but I haven't seen this usage before and don't understand why what appears to be (-1) + (-1) = -1

What am I missing?

1 Answer 1

4

It's the unary plus operator and it converts its operand to a number.

In the code you posted, it'd have no net effect. If, however, it had been this:

var cur = "-1", prv = -1;
prv = +cur;

then "prv" would be set to the numeric value -1 instead of the string that's referenced by "cur".

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

2 Comments

@Pointy-- thanks! To be clear, is this essentially a shorthand way to type cast? Like using parseInt?
@user101289 yes exactly - just a quick way of doing it. If the value can't be converted, of course, you get a NaN.

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.