0

For type conversion, when should one use the primitive wrapper Number() and when should one use parseInt()?

I'm aware that parseInt() allows you to specify a radix, will truncate decimals so that they become integers, and will parse up until the first NaN it encounters in the input. The MDN documentation on the Number() wrapper was less helpful.

My questions:

  1. Is there a general preference for using one or the other?
  2. Are there performance differences?
  3. Are there edge case differences in the two implementations?

Things I've tried:

  1. Reading the parseInt and Number() MDN documentation.
  2. Googling
  3. Searching StackOverflow
  4. Searching StackExchange
phoenixdown
  • 828
  • 1
  • 10
  • 16
  • There's also just the `+` operator which will also do a conversion to a number. I would guess (but don't know for sure) that it's the same as `Number(x)`. – jfriend00 Aug 01 '14 at 07:05
  • 1
    http://stackoverflow.com/questions/4564158/what-is-the-difference-between-parseintstring-and-numberstring-in-javascript – j08691 Aug 04 '14 at 13:51
  • see here http://stackoverflow.com/questions/4564158/what-is-the-difference-between-parseintstring-and-numberstring-in-javascript and here http://stackoverflow.com/questions/4090518/string-to-int-use-parseint-or-number – Mritunjay Aug 04 '14 at 13:52
  • I wish this wouldn't have been marked as a duplicate. I came to this question FROM the supposed duplicate because I had this same question. The linked question describes what each one does but does NOT describe reasons why you should use one in place of another in certain situations. – John Washam Nov 19 '14 at 20:20
  • FYI, @sjngm was kind enough to answer this question in the "EDIT #2" section of [his answer](http://stackoverflow.com/a/4564199/306179) on the linked question. – John Washam Nov 20 '14 at 14:49

0 Answers0