parseInt and other options for parsing numbers in javaScript

In javaScript parseInt is one of several ways to convert a string to a number as an integer. The parseInt method does convert a string or number to an integer, but technically it is still a float as all numbers in javaScript are double precision floating point numbers.

So when using the javaScript parseInt method to produce an integer it is really just a float still technically, but for all practical purposes when it comes to the product in a mathematical sense it is an integer. There are other was to parse to an integer, or float in javaScript as well so lets take a look at the options when it comes to parsing integers in javaScript.

1 - parseInt basic examples

The parseInt method in native core javaScript works by just passing a string of a number, or a number as the first argument. When doing so parseInt will attempt to parse what is passed to it as a number. In addition to giving a value to parse to an integer a radix can be given as a second argument.

1
2
3
4
console.log( parseInt('42') ); // 42
console.log( parseInt('6' * '7') ); // 42
console.log( parseInt(42) ); // 42
console.log( parseInt(42.1234) ); // 42

2 - parseInt converts to String first

The parseInt method might not always return expected results in some situations. For example the parseInt method converts to a string first and if it is a number the goes off into notation, then the letter e char will not be recognized as a number and will parse an int based on what comes before it.

1
2
3
4
5
6
7
8
9
10
11
// parseInt may not always work as expected
console.log(parseInt( Math.pow(10,21) ) ); // 1
// because it converts to string first
let str = String(Math.pow(10,21));
// and it does not consider the letter e to
// be a number
console.log(str); // 1e+21
console.log(parseInt(str)); // 1
console.log( parseInt('12e45') ); // 12

3 - The deal with max safe integer

There is also the nature of the max safe integer, when adding anything to that and going beyond the max safe int that too can result in unexpected results as well.

1
2
3
let n = Number.MAX_SAFE_INTEGER;
console.log(n); // 9007199254740991
console.log(parseInt(n + 100)); // 9007199254741092

4 - parseInt and Number

The Number function can be used to convert a string to a number also. It is a way to explicitly declare the the value that is given to the number function is th be parsed as a number. However it will not parse to an integer, at least not my itself, so it would have to be used in conjunction with an additional method such as the Math.round method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let str = '42.1234';
// number will parse to float if there
// is a fraction
console.log( Number(str) ); // 42.1234
// paser Int will not
console.log( parseInt(str) ); // 42
let str2 = '42abc';
// Number will result in NaN
// if there are non number chars
// in the end of a string
console.log( Number(str2) ); // NaN
// parseInt will not
console.log( parseInt(str2) ); // 42

5 - Multiply by a string of the number 1 and round to parse to an integer

ANother trick that comes to mind is multiply a value by a string of the number one, and then using something like Math.floor, or any other such method to round the result of that. The reason why this works is because of the typeless nature of javaScript. This sort of thing would not work with addition because that is used for both addition and string concatenation. So whe it comes to using addition that would help to convert numbers to strings, and I see similar tricks to this being used as a way to parse numbers to strings. However when it comes to an operator such as multiplication that is something that is only a math operation, so the result is a number rather than a string.

1
2
3
4
5
6
7
var parseToInt = function (a) {
return Math.floor(a * '1');
};
var n = parseToInt('7');
console.log(typeof n); // number
console.log(n); // 7

6 - Conclusion

So the javaScript parseInt method is one of several methods that can be used to parse a value to a number, there are also many other little ticks that can be used to parse a value to a number also. There is the question of not just parsing to a number, but parsing to an integer rather than a float. The parseInt method can be just that method to do so, but I still find myself using the built in Math object rounding methods combined with other methods to insure a number rather than a string.