parseInt()
is a built-in function in JavaScript that parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems) . The function converts its first argument to a string, parses that string, then returns an integer or NaN. If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix. The radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.
Here is an example of using parseInt()
:
const myNumber = 3;
console.log(2 + parseInt(myNumber)); // returns 5
In this example, parseInt()
removes 3 from the string and converts it to an actual number.
Its important to note that parseInt()
should not be used on non-strings, especially as a substitution for Math.trunc()
.