In this tutorial, we will talk about the
NaN (Not a Number).
parseInt function works
The main purpose of using the
parseInt function is to extract a number from a string. This turns the returned value to an actual number.
Here is the syntax:
Consider the example below:
const myNumber = '3'; console.log(2 + myNumber); // returns 23
In the example above, 3 is a string and not an actual number. When we add 2 to the string, we are given 23 because we are just adding 2 to a string that is just in the form of a number.
parseInt function, we can can extract 3 from the string and turn it to an actual number. Here is an example:
const myNumber = '3'; console.log(2 + parseInt(myNumber)); // returns 5
Now the function has removed 3 from the string and converted it to an actual number.
From '3' to 3.
Recall that we said that the
parseInt function can either return an integer or
NaN. So when would we get a
This happens when we have some text before a number in a string. Something like "age is 50" will return a
NaN value because the
parseInt function only looks at the first value starting the string. If the value is not a number,
NaN is returned. Here:
const age = 'age is 50'; console.log(parseInt(age)); // returns NaN
Let's rearrange the string and see what happens.
const age = '50 is the age'; console.log(parseInt(age)); // returns 50
Now the first value in the string is a number and that number is returned to us.
Note that the
parseInt function ignores float values. If the age above was 50.05 then it would still return 50 and ignore .05.
In the same manner, if we had a string like this: "50 100 150 200" then we'd only get 50 returned to us. This is because the
parseInt function only attempts to extract the first value of a string.
And if the string had its values written together like this: '50istheage', 50 would still be returned.
parseInt function accepts a second parameter known as
redix. This parameter specifies what number system to use. If the redix is omitted then 10 is taken as the default.
Here is the syntax:
This is usually an integer between 2 and 36. If the value of the redix is less than 2 or greater than 36 then
NaN is returned.
If we were to specify a redix of 12 then it implies that the number in the string should be parsed from the duodecimal value of the number to its decimal value.
Here is a quick example:
console.log(parseInt("50", 12)); // returns 60
We get 12 returned because the duodecimal value of 50 in base 10 (decimal) is 60.
In this tutorial, we learned how to use the
parseInt function to extract numbers from strings.
We also saw how we could use the
redix parameter to specify what number system to be used when converting our returned integer.
Thank you for reading!