C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part A: We loop over the digits in our string with a for-loop and convert each one to a number.
Part B: We use parseInt, a plus, and the Number constructor. Each string is converted in 3 different ways.
StringsSum: We sum up all the numbers to show that they are valid digits and get the value 45.
JavaScript program that gets integers from string
var codes = "12345";
var sum = 0;
// Part A: loop over chars and convert them to ints.
for (var i = 0; i < codes.length; i++) {
// Part B: apply numeric conversion.
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
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: 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
Version 1: This version of the code uses the plus operator to convert a digit character into an integer.
Version 2: This version uses the parseInt method, which handles more cases of numeric conversion.
Version 3: Here we use the Number() function to get an integer from a string. We use each integer in a switch.
Result: In a Chromium version from 2019 I found that using Number() 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 int parsing
var c = 0;
var codes = "123456";
var x1 = performance.now();
// Version 1: use plus.
for (var i = 0; i < 10000000; 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 x2 = performance.now();
// Version 2: use parseInt.
for (var i = 0; i < 10000000; 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 x3 = performance.now();
// Version 3: use Number.
for (var i = 0; i < 10000000; 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 x4 = performance.now();
// Results.
console.log("TIME 1: " + (x2 - x1));
console.log("TIME 2: " + (x3 - x2));
console.log("TIME 3: " + (x4 - x3));
Output
TIME 1 (2019): 276.049 Plus
TIME 2: 332.764 parseInt
TIME 3: 270.944 Number
Tip: With some JavaScript changes to how data is stored, you can often have the numbers in their native format already.