Advanced use of JavaScript toString() and parseInt()
Tags: beginners, javascript, tutorial, webdev
Published: 2024-01-25 05:29:09

You might have been using toString() method and parseInt() in javaScript in day to day coding life.

One basic use case is like converting a number to string with the help of toString() method

```javascript
const n = 10;
console.log(n.toString())
//Output
//"10"
```
and converting a "10" to number using parseInt().

```javascript
const str = "10";
console.log(parseInt(str));
//Output
//10
```

If you know this means then you understand the basics of toString() and parseInt().


Lets understand it further:

## toString()
When you use .toString()
There is an optional parameter called base.

You might have heard about number systems in Mathematics.

The number system which we use in day to day life is decimal.

A Decimal System uses 10 as base. We can represent it as 
25 -> 2x10^1 + 5x10^0

I am assuming you understand above representation.

There are few famous representations such as Hexadecimal, Octal and Binary. 
Hexadecimal - base 16
Octal - base 8
Binary - base 2

I understand I moved away from the main agenda of this discussion. But the reason behind I was explaining about number system is we can use these bases to convert a number from one type to another.

Lets say we need to convert "25" to binary
```javascript
 const a = 25;
 a.toString(2) 
 // Output
 // 11001
```
Or we want to convert "111" to hexadecimal

```javascript
 const a = 111;
 a.toString(16) 
 // Output
 // 6f
```
Or we want to convert "43" to octal

```javascript
 const a = 43;
 a.toString(8) 
 // Output
 // 53
```

There can be many use cases where you want to do these conversions one example is when you are dealing with buffer and you want to decode.

Lets take another example,
We want to convert text "Hello" to binary

How we will do this ?


```javascript
 Array.from("Hello").map(char => char.charCodeAt(0).toString(2));
// Output
[ "1001000", "1100101", "1101100", "1101100", "1101111" ]

```

If you add prefix to each no with 0b, you can use it with TypedArray View ( Uint8Array, Uint16Array or Uint32Array, etc)

```javascript
const unsignedArray = new Uint8Array([ "0b1001000", "0b1100101", "0b1101100", "0b1101100", "0b1101111" ])
```
You can use it as stream or convert it to blob.

Again I don't want to go deep in Unit8Array. But just for understanding, we can use '0x' as hexadecimal, '0o' for octal.

If you convert this to hexadecimal, then following will be the output

```javascript
Array.from("Hello").map(char => '0x'+char.charCodeAt(0).toString(16)); 
// Output
// [ "0x48", "0x65", "0x6c", "0x6c", "0x6f" ]
```

And I am sure, you must have seen these type of data specially while dealing with buffers.

## parseInt()

So, parseInt() method accepts first parameter as data we want to covert to integer and second optional parameter as base.

Whatever data you pass inside parseInt() it will always try to convert it to a number or you can say to a decimal number to be exact that means its base will be 10.

lets say we have a binary number 1001000 and we want to convert it to decimal. Then we can use parseInt()

```javascript
const num = parseInt(1001000, 2) // base 2

console.log(num)
//OUTPUT
// 72
```
and 72 is ASCII value of 'H' 

```javascript
String.fromCharCode(72)
// Output
// 'H'
```

You can experiment further and use it with other number system like hexadecimal.



If you want to learn about ArrayBuffer, Uint8Array, Uint16Array, etc just comment below.