Unary Plus Operator And ParseInt()
I’ve recently been tutoring a friend of mine in the wide world of programming and JavaScript and while I have a lot to show him, he also occasionally shows me things that I’ve never used. This is why it is good to have an open mind with any concept, as you can definitely learn a lot from a beginner programmer. I was surprised when he started explaining to me the Unary Operator, as I’ve always just used parseInt() or parseFloat().
The Unary Operator is similar to the ParseInt() method in that both can return a number (rather than a string data type). However, there are a few big differences in what the two will return when passed different values. Let’s take a look at these.
How They Are Used?
The first thing I should point out is that the Unary Plus Operator (+) is one of many different arithmetic operators, while ParseInt() is a function that attempts to turn a non-numerical data type into a number. The Unary Plus Operator should appear before the actual number you want to change. This can cause occasional problems if you are adding numbers, but also want to use the Unary Plus Operator, but you can avoid confusing your interpreter by using parentheses. Here are some examples.
parseInt("6")
=> 6+"6"
=> 6
In the above case, they operations both produce the same result. But consider the next case.
6 +6
=>12
6 +"6"
=>"66"In both of these cases, JavaScript interprets the unary symbol as an addition sign (as it should. To be anything else than an addition sign would cause an error). But you can’t just tack on another plus sign (which would also result in an error) like so:
6 ++"6"
=> Uncaught ReferenceError: Invalid left-hand side expression in postfix operationIn order to complete this operation, you would need to wrap your number in parentheses.
6 +(+"6")
=> 12NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
Both the Unary Plus Operator and parseInt() behave differently when passed a value that can’t be converted into a number.
Unary Plus Operator will sometimes return a number representing whether the value is truthy or falsey.
+true
=> 1+false
=> 0+""
=> 0+null
=> 0
However, other times the Unary Plus Operator will return NaN (not a number) for a value it doesn’t deem worthy. From the words of the MDM docs, “If it cannot parse a particular value, it will evaluate to NaN”.
+"bacon"
=> NaNCompared with parseInt(), the Unary Plus Operator allows a bit more freedom.
parseInt(true)
=> NaNparseInt(false)
=> NaNparseInt("")
=> NaNparseInt(null)
=> NaN
The parseInt() function and the Unary Plus Operator also interpret their arguments differently. The Unary Plus Operator is pretty much all or nothing. If it can’t convert the entire argument, then it will return NaN. However, the parseInt() function will return everything up to what it can not parse.
parseInt("55 pigs in a blanket")
=> 55+"55 pigs in a blanket"
=> NaN
Decimals
The parseInt() function has a small problem with decimals.
parseInt(.3)
=> 0
parseInt(3.5)
=> 3Being that an integer is any number without a fractional component, it makes sense that it would literally parse a decimal number into an integer.
If you need to keep the decimal aspect of the number, you can always use parseFloat().
parseFloat('2.899')
=> 2.889Different Base Values
The parseInt() function is able to take a second value which determines the base of the numerical value (like decimal, hexadecimal etc.).
parseInt('10', 16);
=> 16Summary
Both parseInt() and parseFloat() and the Unary Plus Operator(+) can convert a string into a number data type, but both do so in different way and both methods for numerical conversion have differences.
