Convert the string of any base to integer in JavaScript

JavascriptFront End TechnologyObject Oriented Programming

The parseInt function available in JavaScript has the following signature −

parseInt(string, radix);

Where, the paramters are the following −

String − The value to parse. If this argument is not a string, then it is converted to one using the ToString method. Leading whitespace in this argument is ignored.

Radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string.

So we can pass the string and the radix and convert any numbner with base from 2 to 36 to integer using this method.

Example

console.log(parseInt("100", 10))
console.log(parseInt("10", 8))
console.log(parseInt("101", 2))
console.log(parseInt("2FF3", 16))
console.log(parseInt("ZZ", 36))

Output

100
8
5
12275
1295
raja
Published on 16-Sep-2019 11:49:08
Advertisements