Last updated on July 13th, 2020 at 11:41 am
So this is where we stopped in the last video where we so certain cases of ‘not a number’ in the parseInt() function.
So in this video, let’s see what happens if we have a situation whereby the radix is ‘0’, the radix is ‘undefined’ or ‘missing’.
Now if the radix is ‘0’, ‘undefined’ or ‘missing’, number one, if the string begins with ‘0’ and a small ‘x’ or ‘0’ and a capital ‘X’, the radix will be considered as ‘16’.
So maybe if you have some something like ‘0x192’, this will automatically make the radix ‘16’.
‘0x’, ‘x’ stands for hexadecimal.
And number two, if the string begins with ‘0’, the radix will be considered ‘8’ or ‘10’.
And this will depend on the implementation of the browser engine.
So there are certain browsers that will consider it ‘8’, others will consider it ‘10’ if it begins with ‘0’.
And that entirely depends on the browser.
But from ES5, ECMAScript 5 implementations, the default will be 10.
So even if the number begins with zero, the default will be 10.
And then number three, if a string begins with, with any other number, any other value, the radix is always 10.
That means, if it does not begin with ‘0x’ or small ‘x’ or does not begin with a zero, it’ll always be considered 10.
So if it begins with ‘a’, any of the letters from ‘a’ to ‘z’, to ‘z’, or any of the, any of the numbers from ‘1’, from ‘1’ to ‘9’.
In this case, it’ll always be, the radix will always be considered to be 10.
So if I come back here, and let’s start with the first one where we see that if the radix begins with ‘0x’, then the number is considered, the radix here will be considered as 16.
So if I come here and I display, you see this is giving us 20.
That’s because this was given a radix automatically of 16.
If we didn’t have the, if we didn’t have the ‘0X’ and we try to display this, it’ll give us 14.
If you don’t put a radix in there and the number begins with ‘0x’ it’ll be considered as a radix of 16.
So you should always put the radix in there.
And then, the next one we said was that if a number begins with ‘0’, let’s say ‘023’.
If we display this one, it returns ‘23’.
So you can see in this implementation, in this particular implementation of my browser, we are automatically returned ‘23’.
And this means that the radix that is being used is 10.
However in other browsers, it could be different.
But in this particular browser that I’m using, the Firefox engine is giving, is using 10 as a default.
And then, if this one starts with any other number, let’s say it starts with ‘9’ that, and we display, it’ll convert this number based on the radix of 10.
So I hope you’ve understood this.
I mean there’s no easy example to show this.
You only have to understand them the way they are.
If a string begins with ‘0’ and a small ‘x’, so this is hexadecimal.
We’re later on going to see something like ‘0’ and ‘o’ whereby ‘o’ stands for octal.
So we’re going to see that when we look at ‘number literals’.
That is the next section from this, when we start looking at ‘literals’.
So in the past few videos, we have looked at the parseInt() function and how you can use it to convert your numbers.
You can convert any number, any you can convert any string into a number.
So, if this was our number and we use that and we display, that’s what happens.
If that was radix of 16, you see it gives us a different number.
So always display the radix even though in certain modern browsers, it’ll be assumed to be 10, as long as the number doesn’t begin with ‘0x’, always display it there so that it doesn’t confuse the browsers.
So that’s it for this video.
Let’s continue in the next video.
And in the next video, we’re going to start looking at the ‘parseFloat()’.
So, I’ll see you in that next video.
And if you do have any questions, feel free to let me know about them.
See you in the next video.
Click the button below to watch this full course: