JavaScript String to Int: Complete Guide
Data type conversion is a big part of any programming language and converting String to Integer is one of the most used conversions.
JavaScript string to int
To convert a string to an integer in JavaScript, use the parseInt() method. The parseInt() method returns NaN, which means not a number when the string doesn’t hold a number. If the string with a number is sent, then only that number will be returned as an output.
The parseInt() method does not accept spaces. If any specific number with spaces is sent, then the part of a number that presents before space will be returned as an output.
Syntax
parseInt(value, radix);
Parameters
The parseInt() function takes a value as a string and converts it into an integer.
The radix parameter is used to define which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that a number in the string should be parsed from the hexadecimal number to the decimal number.
If the radix parameter is not passed, then JavaScript assumes the following:
- If a string begins with “0x”, the radix is 16 (hexadecimal).
- If a string begins with “0”, the radix is 8 (octal). This feature is deprecated.
- If a string begins with any other value, the radix is 10 (decimal).
Return Value
The parseInt() method parses the string and returns the integer.
If there is no integer present in the string, then NaN will be the output.
You can use the isNaN() method to check if the value is NaN or not.
Example
Write the following program to convert string to int in Javascript.
// app.js let a = parseInt("11") let b = parseInt("11.00") let c = parseInt("11.21") let d = parseInt("11 21 29") let e = parseInt(" 19 ") let f = parseInt("18 years") let g = parseInt("He was 40") console.log(a, typeof (a)) console.log(b, typeof (b)) console.log(c, typeof (c)) console.log(d, typeof (d)) console.log(e, typeof (e)) console.log(f, typeof (f)) console.log(g, typeof (g))
Output
➜ es git:(master) ✗ node app 11 number 11 number 11 number 11 number 19 number 18 number NaN number
In this example, various cases of strings such as only strings, strings with ine, integers have been taken and sent through a parseInt() method.
Javascript Number(): Convert String to Integer
JavaScript Number() is a built-in method that converts a string to an integer but sometimes it’s an integer and other times it’s the point number. If you pass in the string with random text in it, then you will get NaN in the output.
As a result of this inconsistency, it’s not a safe choice than the parseInt() method and parseFloat() functions. If you know the format of a number, you would like, use those instead.
If you want a string to fail with NaN if it has other characters in it, then use the Number() method.
Syntax
Number(value)
Parameters
The Number() method takes a value as a parameter, which is a string.
Return Value
The Number() method returns the integer value of a string.
Example
Write the following code inside the app.js file.
// app.js let a = Number("11") let b = Number("11.00") let c = Number("11.21") let d = Number("11 21 29") let e = Number(" 19 ") let f = Number("18 years") let g = Number("He was 40") console.log(a, typeof (a)) console.log(b, typeof (b)) console.log(c, typeof (c)) console.log(d, typeof (d)) console.log(e, typeof (e)) console.log(f, typeof (f)) console.log(g, typeof (g))
Output
➜ es git:(master) ✗ node app 11 number 11 number 11.21 number NaN number 19 number NaN number NaN number
That is it for this Javascript string to int tutorial.