parseInt() is a built-in JavaScript function that parses integers from numerical strings. For example, let’s parse the integer from the numeric string '100':

const number = parseInt('100');
number; // 100

As expected, '100' is parsed to integer 100.

parseInt(numericalString, radix) also accepts a second argument: the radix at which the numerical string argument is. The radix argument allows you to parse integers from different numerical bases, the most common being 2, 8, 10, and 16.

Let’s use parseInt() to parse a numerical string in base 2:

const number = parseInt('100', 2);
number; // 4

parseInt('100', 2) parses '100' as an integer in numerical base 2: thus it returns the value 4 (in decimal).

That’s pretty much a short introduction to parseInt().

#javascript #number

Solving a Mystery Behavior of parseInt() in JavaScript
1.10 GEEK