29 Javascript ParseInt Two – JavaScript

Last updated on July 13th, 2020 at 11:34 am.

https://www.youtube.com/watch?v=OcDmqf1tzwY
 
This video is taken from the full course that will teach you HTML, CSS, Programming concepts and Javascript .

Transcript :

Hi, and welcome back.

This is where we ended the last video.

And in this video, let’s continue on with parseInt().

How you can convert a number from a string to an integer.

So we’ve said that in the parseInt(), the first thing that you have here is a string.

And then you specify the radix.

So you can see this, this will be converted to a hexadecimal number.

And it’s very important to note that if you don’t define this number, the default in most cases, in most systems, in most interpreters is 10.

But, the general advice is that you should always define the number.

There, even if it’s ‘10’ you should always define it to avoid any misbehaviors depending on the system that your program is running on or rather your application is running on.

So you should always define the radix.

So in this case, this one will be converted to hexadecimal number and then this one will be converted, this will be converted to a decimal number.

So, this one also will be converted to octal number.

So you must note that whatever number that this is, if you’re dealing with a octal number or a binary number or any other radix number, the number whatever is in there must also match the digits which are here.

For example, if this was ‘18’ and you defined this to be converted to this octal number, what would happen is it would convert the first number and then ignore this other one, because this one is not part of their radices which are in an octal number which start from 0 to 7.

So let’s see this as an example so that we can understand exactly what’s going on.

So if I come back to Scratchpad, I remove everything, parseInt().

Let’s say we have a number stored as ‘23’ and we can convert it to a decimal of ‘10’.

So if I display this, it will give us the number of 23.

If I copy that, ‘Control+C’, I bring it there and then if I try.

So you should note that if this is not able to convert the string into a number, then it’ll give you what’s called ‘not a number’.

So if it tries to convert this, and it is not able to convert it, it’ll give you ‘not a number’.

So if I try to display this, it’ll give us ‘not a number’.

And the same thing is true for other kinds of radices.

So let’s say we have ‘03’ right.

And you want to convert this as a binary digit.

We know that numbers in a binary number can only have two digits which are ‘0’ or ‘1’.

So in this case, ‘3’ is not part of the possible numbers in a binary number.

So if I try to convert this, it’ll only give us, it’ll only convert it up to the point that it can convert it and ignore the rest.

And that is the same, that is the same for any of this.

If I take this, and we know ‘23’, that’s ok.

Then maybe ‘235’.

We put an ‘e’ in there.

It will only convert this up to the point that it can.

And then it will ignore the rest.

So ‘23’ is what will be returned to us.

And everything else that cannot be converted will be ignored.

So if I display that, it only returns up to the point that it can convert that number.

You put that there, then you converted it to ‘16’, to a hexadecimal number, you’ll see it will return to us a hexadecimal number.

Display that.

It will return to us ‘291’.

So ‘291’ is a decimal number that is equivalent to hexadecimal number.

So you must always note that the numbers you put here, they must also have the same digits that will be available in here.

So, like if you say ‘abcdef’ this would be converted to a hexadecimal digit.

So let’s just see the result of this.

If I display the result, it’s this one, this is the same decimal number that is equivalent to this hexadecimal number.

So you can, if you’re not understanding this, just go to MDN and read more about this.

I’ll provide the link to the section on the parseInt().

Just go and read more because if you read more about it you’ll understand it better.

But it’s, basically this is how it works.

The numbers which are here must be available within that.

So if I, if I would have added ‘g’ here, we know that ‘g’ is not available in the hexadecimal numbers, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then ‘A’, up to ‘F’.

It doesn’t reach up to ‘G’.

So this is going to get converted up to that point.

And then, it’s going to ignore the rest.

So, and then if we display this, it’ll give us the result of ‘171’ which will, is just these two.

If I remove everything else, and then I display the results of that, you see it’s ‘171’.

So it just does up to the point that it can.

If it encounters something that is not part of that radix, it’s not part of that radix, then it will ignore the rest.

So let’s continue in the next video and see what else we have in parseInt().

Click the button below to watch this full course:

Comment Here

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!