JavaScript(JS) built-in method - parseInt
JavaScript provides the built-in method parseInt()
to parse a string and return an integer. This method takes two arguments: the first argument is the string to be parsed, and the second argument is the radix, which is the base of the number system to be used (for example, 10 for decimal, 16 for hexadecimal, 8 for octal, etc.).
Here is an example of using parseInt()
:
let num1 = parseInt("123"); // returns 123 let num2 = parseInt("1010", 2); // returns 2 (binary number 1010 converted to decimal) let num3 = parseInt("FF", 16); // returns 255 (hexadecimal number FF converted to decimal)
In the first example, the string "123" is parsed as a decimal number and returns the integer value 123.
In the second example, the string "1010" is parsed as a binary number with a radix of 2, which means it is converted to a decimal number with the value 2.
In the third example, the string "FF" is parsed as a hexadecimal number with a radix of 16, which means it is converted to a decimal number with the value 255.