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

16 Jul, 2019
The parseInt() function:The parseInt() function is used to parse a string and convert it to an integer of a specified radix. It takes two parameters, the string to be parsed and the radix to be used. The radix is an integer between 2 and 36 which represents the base of the number.If parseInt() encounters a character while parsing that does not conform to the specified radix, it will ignore the character and all succeeding characters. It then returns the value parsed up to that point as an integer. Spaces that are leading or trailing are allowed in this case.If the function gets the first character and cannot convert it to a number, it will return NaN unless the radix is bigger than 10. This NaN value is not a valid number for any radix and cannot be used in any mathematical calculation.Syntax:parseInt(string, radix)
If parseInt() encounters a character while parsing that does not conform to the specified radix, it will ignore the character and all succeeding characters. It then returns the value parsed up to that point as an integer. Spaces that are leading or trailing are allowed in this case.
If the function gets the first character and cannot convert it to a number, it will return NaN unless the radix is bigger than 10. This NaN value is not a valid number for any radix and cannot be used in any mathematical calculation.
Syntax:
parseInt(string, radix)
The Number() functionThe Number() function is used to create a primitive type Number object. It takes one parameter which is the value of the number. This value could be passed with a string and the Number function will try to represent it as a number. If the argument could not be converted into a number, it returns a NaN value. This NaN value is not a valid number and cannot be used in any mathematical calculation.Syntax:Number(valueString)
Syntax:
Number(valueString)
The differences between these can be explained using these below examples:
Example 1:This example shows that parseInt() tries to convert the value to the last character that could be converted to an integer. The trailing whitespaces and characters are ignored as they are not valid. The Number() function on the other hand just returns NaN.
<!DOCTYPE html><html>  <head>    <title>      What is the difference between       parseInt() and Number()  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>      What is the difference between      parseInt() and Number()  </b>    <p>String is: 10 objects</p>    <p>Output of parseInt is: <span class="parseOutput">      </span>  </p>    <p>Output of Number is: <span class="numberOutput">      </span>  </p>    <button onclick="parseNumber()">      Parse string  </button>    <script type="text/javascript">        function parseNumber() {            let string = '10.6 objects';            let number1 = parseInt(string);            let number2 = Number(string);              document.querySelector(              '.parseOutput').textContent = number1;            document.querySelector(              '.numberOutput').textContent = number2;        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
Example 2:This example shows the difference that parseInt() only returns an integer value whereas Number() returns all the digits including floating points.
<!DOCTYPE html><html>  <head>    <title>      What is the difference between       parseInt() and Number()  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>What is the difference between      parseInt() and Number()  </b>    <p>String is: 3.1415</p>    <p>Output of parseInt is: <span class="parseOutput">      </span>  </p>    <p>Output of Number is: <span class="numberOutput">      </span>  </p>    <button onclick="parseNumber()">Parse string</button>    <script type="text/javascript">        function parseNumber() {            let string = '3.1415';            let number1 = parseInt(string);            let number2 = Number(string);              document.querySelector(              '.parseOutput').textContent = number1;            document.querySelector(              '.numberOutput').textContent = number2;        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
Example 3:This example shows the working of the radix parameter in parseInt(). The string passed is parsed with the base of 2. This returns the value as 12. On the other hand, Number() returns the value in the string as-is.
<!DOCTYPE html><html>  <head>    <title>      What is the difference between       parseInt() and Number()  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>What is the difference between      parseInt() and Number()  </b>    <p>String is: 1100</p>    <p>Output of parseInt is: <span class="parseOutput">            </span></p>    <p>Output of Number is: <span class="numberOutput">      </span></p>    <button onclick="parseNumber()">      Parse string  </button>        <script type="text/javascript">        function parseNumber() {            let string = '1100';            let number1 = parseInt(string, 2);            let number2 = Number(string);              document.querySelector(              '.parseOutput').textContent = number1;            document.querySelector(              '.numberOutput').textContent = number2;        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
JavaScript-Misc
Picked
Difference Between
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.