What is the difference between parseInt(string) and Number(string) in JavaScript? [duplicate]

Loading...

What is the difference between parseInt(string) and Number(string) in JavaScript? [duplicate]

This question already has an answer here:

What is the difference between parseInt() and Number()?

9 answers

What is the difference between parseInt(string) and Number(string) in JavaScript?

Solutions/Answers:

Solution 1:

``````parseInt("123hui")
``````

returns 123

``````Number("123hui")
``````

returns `NaN`

In other words `parseInt()` parses up to the first non-digit and returns whatever it had parsed. `Number()` wants to convert the entire string into a number, which can also be a float BTW.

EDIT #1: Lucero commented about the radix that can be used along with `parseInt()`. As far as that is concerned, please see THE DOCTOR’s answer below (I’m not going to copy that here, the doc shall have a fair share of the fame…).

EDIT #2: Regarding use cases: That’s somewhat written between the lines already. Use `Number()` in cases where you indirectly want to check if the given string completely represents a numeric value, float or integer. `parseInt()/parseFloat()` aren’t that strict as they just parse along and stop when the numeric value stops (radix!), which makes it useful when you need a numeric value at the front “in case there is one” (note that `parseInt("hui")` also returns `NaN`). And the biggest difference is the use of radix that `Number()` doesn’t know of and `parseInt()` may indirectly guess from the given string (that can cause weird results sometimes).

Solution 2:

The first one takes two parameters:

``````parseInt(string, radix)
``````

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

• If the string begins with “0x”, the
radix is 16 (hexadecimal)
• If the string begins with “0”, the
radix is 8 (octal). This feature
is deprecated
• If the string begins with any other
value, the radix is 10 (decimal)

The other function you mentioned takes only one parameter:

``````Number(object)
``````

The Number() function converts the object argument to a number that represents the object’s value.

If the value cannot be converted to a legal number, NaN is returned.

Solution 3:

parseInt(string) will convert a string containing non-numeric characters to a number, as long as the string begins with numeric characters

``````'10px' => 10
``````

Number(string) will return NaN if the string contains any non-numeric characters

``````'10px' => NaN
``````

Solution 4:

The `parseInt` function allows you to specify a radix for the input string and is limited to integer values.

``````parseInt('Z', 36) === 35
``````

The `Number` constructor called as a function will parse the string with a grammar and is limited to base 10 and base 16.

```StringNumericLiteral :::
StrWhiteSpaceopt
StrWhiteSpaceopt StrNumericLiteral StrWhiteSpaceopt

StrWhiteSpace :::
StrWhiteSpaceChar StrWhiteSpaceopt

StrWhiteSpaceChar :::
WhiteSpace
LineTerminator

StrNumericLiteral :::
StrDecimalLiteral
HexIntegerLiteral

StrDecimalLiteral :::
StrUnsignedDecimalLiteral
+ StrUnsignedDecimalLiteral
- StrUnsignedDecimalLiteral

StrUnsignedDecimalLiteral :::
Infinity
DecimalDigits . DecimalDigitsopt ExponentPartopt
. DecimalDigits ExponentPartopt
DecimalDigits ExponentPartopt

DecimalDigits :::
DecimalDigit
DecimalDigits DecimalDigit

DecimalDigit ::: one of
0 1 2 3 4 5 6 7 8 9

ExponentPart :::
ExponentIndicator SignedInteger

ExponentIndicator ::: one of
e E

SignedInteger :::
DecimalDigits
+ DecimalDigits
- DecimalDigits

HexIntegerLiteral :::
0x HexDigit
0X HexDigit
HexIntegerLiteral HexDigit

HexDigit ::: one of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
```

Solution 5:

Addendum to @sjngm’s answer:

They both also ignore whitespace:

var foo = ” 3 “; console.log(parseInt(foo)); // 3
console.log(Number(foo)); // 3

It is not exactly correct. As sjngm wrote parseInt parses string to first number. It is true. But the problem is when you want to parse number separated with whitespace ie. “12 345”. In that case
`parseInt("12 345")` will return `12` instead of `12345`.
So to avoid that situation you must trim whitespaces before parsing to number.
My solution would be:

``````     var number=parseInt("12 345".replace(/\s+/g, ''),10);
``````

Notice one extra thing I used in parseInt() function. `parseInt("string",10)` will set the number to decimal format. If you would parse string like “08” you would get 0 because 8 is not a octal number.Explanation is here

Solution 6:

Addendum to @sjngm’s answer:

They both also ignore whitespace:

``````var foo = "    3     ";
console.log(parseInt(foo)); // 3
console.log(Number(foo)); // 3
``````
Loading...