4

What's the difference between

"2" * 1 + 5

and

parseInt("2") + 5

It's just a to write a more readable code, or there are compatibility issues with the first form.

6
  • jsperf.com/parseint-vs-coercion, parseInt is faster Commented Jan 2, 2013 at 3:10
  • + symbol is shared with joining strings... unless you tell JS "2" is an int the + will just join them to get 25 not 7 Commented Jan 2, 2013 at 3:10
  • parseInt() optionally takes 2 parameters, the second being the "radix" (or "base") the input is in. If omitted, and the input begins with a 0, the base is defaulted to octal (or base-8) which will give undesired results. So, there is at least a side-effect to using the latter in the way you do in your example; a more appropriate way would be parseInt("2", 10) Commented Jan 2, 2013 at 3:11
  • @Dave "2"*1 will get 2 as int Commented Jan 2, 2013 at 3:11
  • Because * is not related to strings so it can only mean mathematical function Commented Jan 2, 2013 at 3:12

2 Answers 2

2

parseInt is used to grab integers from a string. Consider the following code:

var myString = "3 blind mice";
var myInteger = parseInt(myString); //3

JavaScript will do automatic type conversion, so something like this:

"2" * 1 + 5 //7

The string "2" gets converted to a number.

As noted above in the comments, parseInt takes an additional argument for the base.

JavaScript has a lot of very weird rules about type conversion, and sometimes it's not exactly clear what JavaScript will do in every situation. Keep in mind that the + operator is also used for concatenation as well as addition.

If you're trying to explicitly convert something to a number, you can use the Number constructor provided by JavaScript. Considering the following:

var myString = "2";
var myNum = Number(myString); //2
console.log(typeof myNum); //number

Without the new keyword, it can be used to convert strings to numbers. While it does work, I am not sure parseInt should be used for conversion. Just use the Number constructor.

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

5 Comments

"2" + 1 + 5 does not equal 8 it equals 215
I think you mean "2" * 1 + 5 // 7 as "2" + 1 + 5 will print 215.
Yup! That's what I mean. Small typo, my bad.
"2" * 1 + 5 //8 is 7 :P
I messed that up, my bad. I fixed it.
0

I don't think there are compatibility issues with using coercion in the first form, it is a language feature that should be widely supported.

However, since you have to add code to do the conversion either way (* 1 vs. parseInt), I would vote for parseInt from a style perspective because it makes your intention clearer. It is hard enough sometimes to keep types straight in javascript without using implicit conversions.

Someone not familiar with your code or with javascript might wonder what is going on with that first form as well.

Plus, as indicated in the comments, parseInt is faster so it's a win all around.

Comments

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.