Are you confused and having a hard time understanding the integer parseInt in JavaScript?
In this article, you’ll discover the different approaches to converting strings into integers using parseInt() in JavaScript.
So, keep reading to learn how to easily convert a string into an integer in JavaScript. This article explains everything you need to know with clear examples and explanations.
But before we dive into the solutions of converting strings into integers like a pro, let’s first understand integer parseInt() in JavaScript.
What is parseInt() in JavaScript?
The JavaScript parseInt() method or a function is used to parse a string argument and is a handy tool for converting a string into an integer.
It takes two arguments: the first is the string to be parsed, or you want to convert, and the second is an optional radix argument that specifies the numeral system you want to use.
If you don’t provide this second argument or the radix argument, it defaults to 10 (decimal).
This method returns an integer parsed from the given string, or it returns NaN, which stands for “Not a Number.”
So, for instance, if you have a string that contains the number “18,” and you want to convert it into an integer, you could use parseInt(“18”), and it would return the integer 18.
In simple words, the parseInt() in JavaScript parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
Syntax
parseInt(string)
parseInt(string, radix)
Parameters
📌string or value (required)
A string starting with an integer or the value to be parsed.
📌radix (optional)
A number (2 to 36) that displays the radix (the base in mathematical numeral systems).
Return value
This method returns an integer parsed from the given string, or it returns NaN, which stands for “Not a Number” if no integer is found.
How to use integer parseInt in JavaScript?
As what we mentioned earlier, the parseInt() in JavaScript is used to parse a string argument and convert it into an integer of the specified radix (the base in mathematical numeral systems).
Here’s an example of how to use the parseInt() method in JavaScript:
let str = "18";
let num = parseInt(str);
console.log(num);
We use the parseInt() to convert this string into an integer and store the result in the variable num. When we log the value of num to the console, we see that it is indeed the integer 18.
Output:
18
As you can see, in this example, we have a string str that contains the number “18.”
The parseInt() takes two arguments: the first is the string to be parsed, and the second is an optional radix argument that specifies the numeral system to be used. If the radix argument is not provided, it defaults to 10 (decimal).
Here’s an example that shows how to use the radix argument:
let str = "1010";
let num = parseInt(str, 2);
console.log(num);
As you can see, in this example, we have a string str that contains the binary representation of the number 10. We use the parseInt() with a radix argument of 2 to specify that we want to parse the string as a binary number.
The method converts the binary string into an integer and stores the result in the variable num. When we log the value of num to the console, we see that it is indeed the integer 10.
Output:
10
Different ways on how to convert a string into number or interger in JavaScript
In JavaScript, there are several ways to convert a string into a number or an integer. Here are some common methods:
Solution 1: Use the parseInt() function
This function takes a string as an argument and returns an integer. If the string cannot be converted to an integer, it returns NaN.
For example:
let str = "3.14";
let num = parseInt(str);✅
console.log(num);
Output:
3
Solution 2: Use the Number() function
This function takes a string as an argument and returns a number. If the string cannot be converted to a number, it returns NaN (Not a Number).
For example:
let str = "18";
let num = Number(str);✅
console.log(num);
Output:
18
Solution 3: Use the unary plus operator (+)
This operator can be used to convert a string into a number by placing it before the string. If the string cannot be converted to a number, it returns NaN.
For example:
let str = "18";
let num = +str; ✅
console.log(num);
Output:
18
Solution 4: the parseFloat() function
This function takes a string as an argument and returns a floating-point number. If the string cannot be converted to a floating-point number, it returns NaN.
For example:
let str = "4.5";
let num = parseFloat(str); ✅
console.log(num);
4.5
Solution 5: Use the Math.floor() function
This function takes a number as an argument and returns the largest integer less than or equal to the given number.
You can use this function in combination with the parseFloat() function to convert a string into an integer.
For example:
let str = "5.5";
let num = Math.floor(parseFloat(str)); ✅
console.log(num);
Output:
5
Solution 6: Use the Math.round() function
This function takes a number as an argument and returns the value of the number rounded to the nearest integer. You can use this function in combination with the parseFloat() function to convert a string into an integer.
For example:
let str = "5.5";
let num = Math.round(parseFloat(str)); ✅
console.log(num);
Output:
6
Solution 7: Use the Math.ceil() function
The Math.ceil() function takes a number as an argument and returns the smallest integer greater than or equal to the given number. You can use this function in combination with the parseFloat() function to convert a string into an integer.
For example:
let str = "6.5";
let num = Math.ceil(parseFloat(str)); ✅
console.log(num);
Output:
7
What is the difference between number parseInt () and parseInt () in JavaScript?
The Number.parseInt() and parseInt() are the same function in JavaScript. Both are used to parse a string argument and convert it into an integer of the specified radix (the base in mathematical numeral systems).
The parseInt() function is a global function, while Number.parseInt() is a method of the Number object. However, they both refer to the same function and can be used interchangeably.
Here’s an example that shows how to use both parseInt() and Number.parseInt():
let str = "18";
let num1 = parseInt(str);✅
let num2 = Number.parseInt(str);✅
console.log(num1); // 18
console.log(num2); // 18
In this example, we use both parseInt() and Number.parseInt() to parse the string “18” and convert it into an integer.
Output:
18
18
Conclusion
In conclusion, this article explores different methods and functions on how to convert a string into an integer in JavaScript using the parseInt() function.
The parseInt() function is a versatile tool that allows you to convert strings to integers based on different numeral systems (radix).
Apart from that, this article also discusses other methods for converting strings to numbers or integers aside from parseInt(), such as Number(), the unary plus operator (+), parseFloat(), Math.floor(), Math.round(), and Math.ceil().
We are hoping that this article provides you with enough information that help you understand the integer parseInt in JavaScript.
You can also check out the following article:
Thank you for reading itsourcecoders 😊.