Converting strings to numbers with vanilla JavaScript

JavascriptFront End TechnologyObject Oriented Programming

The parseInt function available in JavaScript has the following signature −

Syntax

parseInt(string, radix);

Where the parameters 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 number with base from 2 to 36 to integer using this method. For example,

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

This will give the output −

100
85
12275
1295
Published on 02-Dec-2019 05:40:38