ParseInt vs Number in JavaScript: Key Differences and Usage
parseInt converts a string to an integer by reading digits until a non-digit appears, ignoring decimals, while Number converts the entire string to a number including decimals and returns NaN if invalid. Use parseInt for integer parsing and Number for full numeric conversion.Quick Comparison
This table summarizes the main differences between parseInt and Number in JavaScript.
| Feature | parseInt | Number |
|---|---|---|
| Type of conversion | Parses integer from string, stops at first non-digit | Converts whole string to number (integer or float) |
| Handles decimals | Ignores decimals, returns integer only | Parses decimals correctly |
| Returns on invalid input | Returns parsed integer up to invalid character or NaN if none | Returns NaN if entire string is invalid |
| Input types | Usually string, ignores trailing characters after digits | Converts string, boolean, null, or other types to number |
| Radix support | Supports optional radix parameter to specify base | No radix parameter, always base 10 for strings |
| Whitespace handling | Ignores leading whitespace | Ignores leading and trailing whitespace |
Key Differences
parseInt reads a string from left to right and converts as many numeric characters as possible into an integer. It stops parsing when it encounters a non-numeric character, so it can parse strings like "123abc" as 123. It also accepts a second argument called radix to specify the number base (like 10 for decimal or 16 for hexadecimal).
On the other hand, Number tries to convert the entire string into a number, including decimals. If the string contains any invalid characters or is not a valid number format, it returns NaN. It does not accept a radix parameter and treats strings as base 10 by default.
Additionally, Number can convert other types like booleans (true to 1, false to 0) and null to 0, while parseInt is mainly used for strings. This makes Number more versatile but stricter in parsing full numeric values.
Code Comparison
console.log(parseInt('123.45')); // 123 console.log(parseInt('123abc')); // 123 console.log(parseInt('abc123')); // NaN console.log(parseInt(' 42 ')); // 42 console.log(parseInt('0xF', 16)); // 15
Number Equivalent
console.log(Number('123.45')); // 123.45 console.log(Number('123abc')); // NaN console.log(Number('abc123')); // NaN console.log(Number(' 42 ')); // 42 console.log(Number('0xF')); // 15 console.log(Number(true)); // 1 console.log(Number(false)); // 0 console.log(Number(null)); // 0
When to Use Which
Choose parseInt when you need to extract an integer from the start of a string, especially if the string may contain extra characters after the number or if you want to specify a number base (radix).
Choose Number when you want to convert a whole string or other types into a number, including decimals, and want strict validation that the entire input is a valid number.
In summary, use parseInt for flexible integer parsing and Number for precise full numeric conversion.
Key Takeaways
parseInt parses integers from strings and stops at the first invalid character.Number converts entire strings or values to numbers, including decimals, or returns NaN if invalid.parseInt supports a radix parameter; Number does not.parseInt for integer extraction and Number for full numeric conversion.Number can convert booleans and null, while parseInt is mainly for strings.