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

Tutorialsrack 27/01/2021

In this article, you will learn what is the difference between parseInt() and Number() in Javascript. Basically, Both the functions Number() and parseInt() are often used to convert a string to a number. While Number() and parseInt() may look similar but there are a few major differences between them that may lead to confusion and bugs. So in this article, we will discuss each one of them.

Number() Function

The Number() function converts the object argument to a number that represents the object's value. If the value is not converted to a valid number then it will return NaN.

Number() function coerces any value to a number (be it a string, boolean, object, etc), and uses type conversion rules for that value.

parseInt() Function 

The parseInt() function parses a string and returns an integer. 

parseInt() function expects a string input (if it’s not a string, it’s converted to a string first). parseInt() function also accepts an optional second argument that tells the string’s number system (binary, hexadecimal, etc).

Difference Between Both the Function

#1. The Number() function converts the type whereas parseInt() function parses the value of the input.

Example 1
// Parsing
console.log(parseInt("32px"));   //it will return 32
console.log(parseInt("5e1"));    //it will return 5

// Convert type
console.log(Number('32px'));     //it will return NaN
console.log(Number('5e1'));      //it will return 50

#2. parseInt() function accepts two parameters and the second parameter is optional. The second parameter is used to indicate the radix number. Whereas the Number() function takes one parameter.

Example 2
console.log(parseInt('0101'));       //it will return 101
console.log(parseInt('0101', 10));   //it will return 101
console.log(parseInt('0101', 2));    //it will return 5  
console.log(parseInt('0101', 8));    //it will return 65  

console.log(Number('0101'));         //it will return 101

#3. Both the parseInt() and Number() function return different results when we passing special values such as undefined or null or '':

Example 3
console.log(parseInt());         //it will return NaN
console.log(parseInt(null));     //it will return NaN
console.log(Number(undefined));  //it will return NaN
console.log(parseInt(true));     //it will return NaN
console.log(parseInt(""));       //it will return NaN
console.log(Number());           //it will return 0
console.log(Number(null));       //it will return 0
console.log(Number(undefined));  //it will return NaN
console.log(Number(true));       //it will return 1
console.log(Number(""));         //it will return 0

#4. If we pass floating-point literals to parseInt() function and it returns the integer. We should note that parseInt() function doesn’t round the value but simply returns the integer part only. Whereas Number() function returns the floating-point number. 

Example 4
console.log(parseInt('15.755')); //it will return 15
console.log(Number('15.755'));   //it will return 15.75

When to Use Number() Function and When to Use parseInt() Function

If you’re converting from a different number system to the familiar base-10  number system, you’d use parseInt() function. For example, if you want to convert the hexadecimal (base-16) value 'a7', you’d use parseInt('a7', 16). Otherwise, you can use Number() function

I hope this article will help you to understand what is the difference between parseInt() and Number() in Javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!

Related Posts


Recent Posts