parseInt() And Number() In Javascript Made Easy

In JavaScript, you can use parseInt() or number() methods to convert string to a number. But There is a slight difference between how these methods work.

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.

What are the differences between parseInt() and Number() in Javascript

  1. parseInt() and Number() are both used to convert a string into a number.
  2. parseInt() parses the value of the string and converts to number till the first non-digit character.
  3. number() tries to convert the type of the string to number and returns nothing if there are non-digit characters in the string.
// returns 67 
// Convert type
parseInt("67dpi");   

// returns NaN
Number('67dpi');

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *