I'm in a course to apply to a Programming school but for more I've searched, I can not understand the parseInt, could someone explain to me?
I'm in a course to apply to a Programming school but for more I've searched, I can not understand the parseInt, could someone explain to me?
The MDN documentation has good information about the function parseInt
.
Let's assume you have a text string with a number:
let numeroA = '10';
The variable numeroA
contains a text string string
, which contains the characters 11
. But this is not a number, it is a string of text which turns out to have numeric characters.
The parseInt()
function can take this value and convert it to a number.
let numeroB = parseInt(numeroA);
Which will store a numeric data type in the variable numeroB
.
The function parseInt()
has many options like converting numbers in different bases (decimal, octal, hexadecimal), check if what is entered can not be interpreted as a number (for which the function would return a NAN
) .