Difference between parseInt() And Number() In Javascript
In JavaScript, you can use either parseInt() or number() methods to convert string to a number. There is a slight difference between how these methods work and when you should use which method for the
In JavaScript, you can use either parseInt() or number() methods to convert string to a number. There is a slight difference between how these methods work and when you should use which method for the use case.
What is parseInt() function
parseInt() method converts an input string to a number. The method takes two parameters - a string literal and an optional radix. Based on the radix, the string is converted into a number of that notation.
By default, the radix is set to 10, which is decimal number.
For example, you can pass a string of hexadecimal notation and pass the radix as 16. The result will be a number equivalent to the hexadecimal number. You can thus use this method to convert any string to decimal format number, even if the string is of a different notation.
// This is a hexadecimal number
let hexString = "1a";
// Specifying radix 16 for hexadecimal
let decimalNumberWithRadix = parseInt(hexString, 16);
// Output: Original Hex String: 1a
console.log("Original String:", hexString);
// Output: Parsed Decimal Number: 26
console.log("Parsed Number:", decimalNumberWithRadix);On the other hand, the parseInt() method also converts an input string to an equivalent decimal number till the first non-digit character.
That means if in the above example if we don't pass the radix as 16 for the hexString, the result is 1, because in the string literal passed 'a' is not a number.
If the first character itself cannot be converted to a number, it will return NaN (Not a Number).
// Specifying radix 16 for hexadecimal
let decimalNumberWithoutRadix = parseInt(hexString);
// Output: Parsed Decimal Number: 1
console.log("Parsed Number:", decimalNumberWithoutRadix);What is Number() function
Number() function is another built-in function that can convert a string to a number. This function doesn't convert any string literal that contains non-numeric literal in any position and returns an NaN (Not a Number) error.
let strNumber = "123";
let num = Number(strNumber);
// Output: Original String: 123
console.log("Original String:", strNumber);
// Output: Converted Number: 123
console.log("Converted Number:", num);
let nonNumericString = "abc";
let result = Number(nonNumericString);
// Output: Original String: abc
console.log("Original String:", nonNumericString);
// Output: Converted Number: NaN
console.log("Converted Number:", result);
// Converting a boolean to a number
let boolValue = true;
let numFromBool = Number(boolValue);
// Output: Original Boolean Value: true
console.log("Original Boolean Value:", boolValue);
// Output: Converted Number: 1
console.log("Converted Number:", numFromBool);You can thus use Number() function to convert any string literal to number, if the string is a proper number format. This function doesn't have any radix specification and doesn't partially convert a literal.
parseInt() vs Number() - When to use which?
Both these functions are used to convert a string into a number.
parseInt() parses the value of the string and converts to number till the first non-digit character.
It also has support to pass and convert an input string to any radix other than decimal.
number() tries to convert the type of the string to number and returns nothing if there are non-digit characters in the string.
If you need a strict type check on the number type, use Number(). Looking for option to convert from a different radix, use parseInt().
Checkout the original post here - https://referbruv.com/programming-questions/what-is-the-difference-between-parseint-and-number/
From the Author -
Hello there, thank you very much for subscribing to my newsletter. I’ll try to write more such informative posts regularly every monday starting next week, and share my knowledge with you.
Please do let me know your feedback or any suggestions, it’d mean a lot to me.
Let’s learn and grow together, one Post at a time.
Happy New Year 2024!
Learn the skills you need to make 2024 your year. Save up to 35% during Udemy New Year’s Sale. Click the link to know more.

