tags:

views:

637

answers:

5

While looking at the jslint code conventions I saw this line:

total = subtotal + (+myInput.value);

What is the purpose of the second '+'?

+30  A: 

The unary plus is there for completeness, compared with the familiar unary minus (-x). However it has the side effect, relied upon here, of casting myInput.value into a Number, if it's something else such as a String:

alert(1+'2'); // 12
alert(1+(+'2')); // 3
bobince
Thanks, I didn't know this notation. I assume this is basically shorthand for parseInt()?
Luke Dennis
They're not quite the same, Luke, see here for some information: http://alexle.net/archives/290
Chad Birch
Plus of course it would be closer to ‘parseFloat’. +-casting supports full floating point number literals, including weirdnesses like +'Infinity'.
bobince
actually, the thing `(+foo)` is closest to is the explicit cast `Number(foo)` - in fact, they should yield identical results
Christoph
Is there any reason why this should be preferred over parseInt?
toast
@toast: they do different things: `Number()` and `+` will also convert floats and return `NaN` on invalid input, whereas `parseInt()` will just stop parsing on the first non-numeric character, ie `parseInt('42foo')` is `42`, whereas `(+'42foo')` is `NaN`
Christoph
@Christoph: That's handy. I can see where they each could be used and why one would prefer a certain way for different conditions. Thanks
toast
which is better in terms of execution time?
kunjaan
In Firefox/Chrome, parseInt comes out 25% faster. In IE/Safari, it takes four times as long. Opera, twice as long. So there's no one conclusion you can draw. In any case, the amount of time it takes is minuscule; choosing one over the other on that ground would be totally a premature optimisation.
bobince
@Luke Dennis: Want a shortcut for ‘parseInt(x, 10)’? Use ‘~~x’.
Mathias Bynens
@Mathias: that's a 32-bit bitwise op, so it will give different results to `parseInt` for numbers out of range of a 32-bit signed int. eg. `~~'2147483648'`->`-2147483648`
bobince
+8  A: 

That's called the "unary + operator", it can be used as a quick way to force a variable to be converted to a number, so that it can be used in a math operation.

Chad Birch
+2  A: 

The + is to typecast it to a number as others have said. It's needed there because form inputs are always string values, and adding a string to another variable concatenates them together into a new string, even if the string looks like a number.

Ant P.
+3  A: 

The unary + operator turns things into a number.

Ryan Graham
+1  A: 

The unary plus operator is, arithmetically speaking, a noop. But like all other purely arithmetic operators, it will convert its argument to JavaScript's number type and can therefore be used as a shorthand for an explicit cast.

Explicit type casting in JavaScript is done via calling the appropriate constructor functions without using the new operator.

For example,

Number(foo)

will convert foo to a primitive of type number, whereas new Number(foo) would additionally create a wrapper object for the primitive and is therefore equivalent to

Object(Number(foo))

Similar to this use of + is the use of !! to convert to boolean type.

Christoph