Table of contents
1.
Introduction 
2.
ParseInt Javascript Function
3.
Syntax of Javascript parseInt() 
3.1.
Javascript
4.
Parameters of Javascript parseInt()
4.1.
String
4.2.
Radix
5.
Return Value of Javascript parseInt() 
6.
Exceptions of ParseInt Javascript
6.1.
Input string contains non-numeric characters
6.2.
Javascript
6.3.
Radix not within range (2-36)
6.4.
Javascript
7.
Working of ParseInt Javascript Function
8.
Implementation of Javascript parseInt()
8.1.
Code
8.2.
Javascript
8.3.
Output
9.
Javascript parseInt() Method Example
9.1.
Adding Two Strings Without Using parseInt()
9.2.
Javascript
9.3.
Passing Radix Argument within Parseint Javascript
9.4.
Javascript
10.
Frequently Asked Questions
10.1.
What is parseInt () in JavaScript? 
10.2.
How do you use parseInt?
10.3.
What does number parseInt() do?
10.4.
Why use parseInt() in JavaScript?
11.
Conclusion
Last Updated: Sep 25, 2024
Easy

JavaScript parseint() Method

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Javascript is a programming language that can be used on both the client-side and server-side of web development. It makes it possible to create interactive web pages by giving HTML and CSS functionality and interactivity. While HTML and CSS are in charge of the organization and aesthetics of web pages, JavaScript adds interactive elements that keep visitors interested.

JavaScript parseInt() Method

In this article, we will learn about the parseint javascript. It is one of its built-in functions. You can turn a string into an integer using this Function. 

Let's start with the parseint javascript function.

ParseInt Javascript Function

parseInt javascript function converts a string argument to an integer. It accepts a radix parameter to specify the number system used, with a default of base 10. NaN is returned if the string cannot be converted to a number. It parses only the first number encountered in the string argument and any non-numeric characters before it results in NaN. To avoid this, the Number() constructor can be used instead.

Syntax of Javascript parseInt() 

  • Javascript

Javascript

parseInt(string, radix)
You can also try this code with Online Javascript Compiler
Run Code

Parameters of Javascript parseInt()

It has two parameters:

String

The string is a mandatory parameter as strings convert to an integer.

Radix

The radix parameter is optional and specifies the base of the numeral system used in the string. The radix can be any integer value between 2 and 36. If the radix is not in this range or is not an integer, the function will return NaN.

  • If the string begins with "0x" or "0X", the radix is 16 (hexadecimal).
  • If the string has any other value, the radix is 10(decimal)

Return Value of Javascript parseInt() 

The function return number or NaN(Not a Number).

  • If the string cannot be converted to a number, parseInt() returns NaN.
     
  • If the radix parameter specified in parseInt() is not an integer or is not within the range of 2 to 36, parseInt() returns NaN.

Exceptions of ParseInt Javascript

The parseInt() function does not generate exceptions by default in JavaScript. Instead, it returns NaN (Not a Number) when it cannot convert the input string to a number. However, there are certain situations where unexpected behavior can arise, which may be viewed as "exceptions." To clarify, these behaviors are not true exceptions because they do not interrupt program execution or cause it to terminate abruptly. Nonetheless, they can cause unexpected results and must be handled accordingly.

Input string contains non-numeric characters

If the input string passed to parseInt() contains non-numeric characters (other than a leading '+' or '-'), parseInt() will stop parsing the string at the first non-numeric character and return the parsed Number up to that point.

Example:

  • Javascript

Javascript

parseInt("346pqr");  
// Returns 346 (stops parsing at 'p')
parseInt("52.7");  
// Returns 52 (stops parsing at the decimal point)
You can also try this code with Online Javascript Compiler
Run Code

Radix not within range (2-36)

If the radix parameter specified in parseInt() is not an integer or is not within the range of 2 to 36, parseInt() returns NaN.

Example:

  • Javascript

Javascript

parseInt("1010", 1);  
// Returns NaN (invalid radix)
parseInt("1010", 50);  
// Returns NaN (invalid radix)
You can also try this code with Online Javascript Compiler
Run Code

Working of ParseInt Javascript Function

The parseInt function in JavaScript is used to parse a string and convert it into an integer. The javascript method accepts two parameters: the string to be parsed and an optional radix or the base value.

The function starts by ignoring any whitespace characters that may be present at the beginning of the string. It then checks if the first character is a sign (+ or -) and then adjusts the sign of the resulting integer accordingly. Next, it iterates through the remaining characters in the string from left to right and converts them to their integer values based on the radix. If the radix is not specified, a default value of base 10 is considered.

The parsing continues until a non-numeric character is reached. At this point, the function stops and returns the collected integer value. If no valid integer can be parsed, it returns NaN (Not a Number).

Implementation of Javascript parseInt()

Code

  • Javascript

Javascript

let userInput = prompt("Enter a number:");
let num = parseInt(userInput);
console.log(num);
You can also try this code with Online Javascript Compiler
Run Code

Output

Enter a number:32w
32


You can practice by yourself with the help of Online Javascript Compiler for better understanding.

Javascript parseInt() Method Example

Adding Two Strings Without Using parseInt()

The + operator can be used to concatenate strings, so adding two strings using the + operator results in the concatenation of the two strings, rather than mathematical addition.

Code:

  • Javascript

Javascript

const string1 = prompt("Enter the first number:");
const string2 = prompt("Enter the second number:");

// Using parseInt()
const result1 = parseInt(string1) + parseInt(string2);
console.log(`Using parseInt(): ${result1}`);

// Using the + operator
const result2 = string1 + string2;
console.log(`Using the + operator: ${result2}`);
You can also try this code with Online Javascript Compiler
Run Code


Output:

Enter the first number:45
Enter the second number:32
Using parseInt(): 77
Using the + operator: 4532

Passing Radix Argument within Parseint Javascript

The radix argument within the parseInt() method specifies the numerical base of the string being parsed, and it is an optional parameter.

Passing Radix Argument within Parseint Javascript

Code:

  • Javascript

Javascript

let binaryNumber = "101101";
let decimalNumber = parseInt(binaryNumber, 2);
console.log(decimalNumber);
You can also try this code with Online Javascript Compiler
Run Code

Output:

45

Frequently Asked Questions

What is parseInt () in JavaScript? 

The parseInt() method in javascript converts a string argument to an integer. It accepts a radix parameter to specify the number system used. NaN is returned if the string cannot be converted to a number.

How do you use parseInt?

To use parseInt in JavaScript, you must pass a string as the first argument and an optional radix (to specify the number system) as the second. The syntax of the parseInt() method in javascript is "parseInt(string, radix)."

What does number parseInt() do?

The number parseInt() function in JavaScript takes a string as an argument and converts it into an integer. The parseInt() method also takes an optional "radix: argument to specify the base used for parsing.

Why use parseInt() in JavaScript?

The parseInt() function is used to convert strings into integers. It can be helpful in certain situations like data conversions, removing leading zeroes, converting numbers into numeral systems with different bases etc.

Conclusion

The parseInt function in javascript is useful for converting strings to integers in JavaScript. It is widely used in web development for handling user input, form data, and other data types that need to be converted from a string to an integer. 

Suppose you want to know more about the parseint javascript and functions like this. In that case, refer to the following articles:

Happy Learning!

Live masterclass