Unary Plus Operator Shorthand for Converting a String to a Number in Javascript

If you are a javascript developer like me, you must have come across a scenario where you needed to convert a string to a number. I have generally been using either parseInt or parseFloat for these type of conversions.

For example, you would do something like this for the conversion

1
2
  var num = parseInt("10");
 // output : num = 10;
1
2
   var num = parseFloat("10.23")
  // output: num = 10.23

Most recently I have come across another interesting way to do the same conversion, i.e. using the unary plus operator ( + ). The above two statements can be rewritten as following to achieve the same result

1
2
var num = +"10"
// output : num = 10;
1
2
   var num =  +"10.23"
  // output: num = 10.23

Although choosing one style of conversion might be a personal opinion, I think that converting using the unary plus operator would act as a great shorthand, especially while writing unit test for you javascript code.

Comments