This is for anyone that's newly immersing themselves in the world of coding. If you are a beginner-level coder like me, you've looked into
parseInt() and became completely lost when it came to the "radix" part. Here is my simplest explanation of what the function
parseInt is a function that can take 2 arguments and it can either return an integer or a NaN (not a number).
This is the syntax:
Assuming you know all the different data types, for example:
1 + 2 + 3; //6
'1' + 2 + 3 // '123'
Now consider this example:
parseInt will remove the quotes from the string before performing the addition or any form of operation. Thus resulting in a whole integer.
If ever you need a floating integer (a number with a decimal) to convert into a whole number,
parseInt can arrange that for you as well.
const num = 10.03 console.log(parseInt(num)); // 10
parseInt should take 2 arguments. The function parses (analyzes) a string and returns an integer of the specified radix.
A radix is the base in mathematical numeral systems. When using
parseInt, you will want to work with the base 10 because it uses the ten digits from 0 through 9. This is called the decimal system.
parseInt("4", 10) // 4 parseInt("4.444", 10) // 4 parseInt("Jerry is 9", 10) // NaN
The last thing you need to know is NaN. This function can only read the first value starting the string. If it's not a number, it will return as NaN.
“Radix.” Wikipedia, Wikimedia Foundation, 23 June 2022, https://en.wikipedia.org/wiki/Radix.