# JavaScript parseInt Examples

Convert strings to ints with the parseInt function. Use Number and parseFloat.
ParseInt. A program sometimes has a string but needs an int. For example in numerical operations, ints can be used but strings cannot be.
Conversion syntax. In JavaScript we can convert a string to an int with parseInt, a built-in method. Another option is with plus (to implicitly convert to an int).
A program. Here we have a string with 5 digits in it. We loop over those digits with a for-loop and convert each one to a number. We use parseInt, a plus, and the Number constructor.

Sum: We sum up all the numbers to show that they are valid digits and get the value 45.

JavaScript program that uses parseInt, plus, Number var codes = "12345"; var sum = 0; // Loop over chars and convert them to ints. for (var i = 0; i < codes.length; i++) { var test1 = parseInt(codes[i]); var test2 = +codes[i]; var test3 = Number(codes[i]); // Write our ints. console.log("INT: " + test1); console.log("INT: " + test2); console.log("INT: " + test3); // Sum the total. sum += test1 + test2 + test3; } // Write the sum. console.log("SUM: " + sum); Output INT: 1 INT: 1 INT: 1 INT: 2 INT: 2 INT: 2 INT: 3 INT: 3 INT: 3 INT: 4 INT: 4 INT: 4 INT: 5 INT: 5 INT: 5 SUM: 45
ParseFloat. Suppose we have a number with a fractional part like 1.5. This is a floating-point number. We need to use the parseFloat method to retain the fractional part.

Here: We use parseInt, which discards everything after the decimal place, and parseFloat.

JavaScript program that uses parseFloat var test = "100.534"; var resultInt = parseInt(test); console.log("PARSEINT: " + resultInt); var resultFloat = parseFloat(test); console.log("PARSEFLOAT: " + resultFloat) Output PARSEINT: 100 PARSEFLOAT: 100.534
IsNaN. Sometimes a string cannot be parsed. It may contain a word or other text. Here the parseInt method returns NaN, a special value in JavaScript.

IsNan: We can use the isNaN built-in method to test to see if nothing was parsed by parseInt.

JavaScript program that uses parseInt, isNaN // This string cannot be parsed. var test = "abc"; var result = parseInt(test); console.log("PARSEINT: " + result); // We can detect not a number with isNaN. if (isNaN(result)) { console.log("ISNAN"); } Output PARSEINT: NaN ISNAN
Performance, parseInt. Suppose we do not want our web page to be extremely slow. Which method should we use to convert strings to ints? I tested them in a loop with a switch.

Result: In a Google Chrome version from 2016 I found that using a plus prefix is the fastest approach to converting a string to an int.

And: Using parseInt took about twice the time. So avoiding parseInt unless needed is a good approach.

JavaScript program that benchmarks plus var t1 = performance.now(); var c = 0; var codes = "123456"; for (var i = 0; i < 1000000; i++) { for (var z = 0; z < codes.length; z++) { switch (+codes[z]) { case 1: c++; break; case 2: c--; break; case 3: c += 2; break; default: c = 0; break; } } } var t2 = performance.now(); document.title = t2 - t1 + "/" + c; JavaScript program that benchmarks parseInt var t1 = performance.now(); var c = 0; var codes = "123456"; for (var i = 0; i < 1000000; i++) { for (var z = 0; z < codes.length; z++) { switch (parseInt(codes[z])) { case 1: c++; break; case 2: c--; break; case 3: c += 2; break; default: c = 0; break; } } } var t2 = performance.now(); document.title = t2 - t1 + "/" + c; JavaScript program that benchmarks Number var t1 = performance.now(); var c = 0; var codes = "123456"; for (var i = 0; i < 1000000; i++) { for (var z = 0; z < codes.length; z++) { switch (Number(codes[z])) { case 1: c++; break; case 2: c--; break; case 3: c += 2; break; default: c = 0; break; } } } var t2 = performance.now(); document.title = t2 - t1 + "/" + c; Output 41.78 ms + prefix  41.17 ms  85.77 ms parseInt  86.50 ms  49.58 ms Number  48.94 ms 
Notes, parseInt. Now I have found that using a plus to convert a string to an int is a fast solution. But the fastest approach is to do no conversion at all.

Tip: With some JavaScript changes to how data is stored, you can often have the numbers in their native format already.

When necessary, a plus sign provides fast string to int conversions in Google Chrome. Other modern browsers seem to have similar benefits here.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls