This JavaScript tutorial explains how to use the Number technique called toString() with syntax and examples.

## Description

In JavaScript, toString() is a Number approach that is used to convert a number to a string illustration of the number. Because toString() is a method of the Number object, it need to be invoked thru a unique occasion of the Number class.

## Syntax

In JavaScript, the syntax for the toString() method is:

`number.toString([radix]);`

Parameters or Arguments

radix Optional. It represents the radix or base of the mathematical numeral device to use to convert the number to a string representation of the number. It can be an integer value between 2 and 36. For example, a radix of 2 would suggest the range need to be returned as a string representation of a Binary number, a radix of 10 would imply that the variety must be returned as a string illustration of a Decimal number, and so on. If no radix is provided, the quantity will be lower back as a string representation of a Decimal number.

## Returns

The toString() approach returns a string illustration of a range (in the mathematical numeral device distinctive by using the optionally available radix parameter).

## Note

In mathematical numeral systems, the radix specifies the range of special digitals in the numeral gadget inclusive of zero For example, a radix of 10 would represent the Decimal gadget due to the fact the Decimal gadget uses ten digits from 0 through 9. Whereas, a radix of 2 would characterize the Binary device due to the fact the Binary device makes use of only two digits which are zero and 1. The toString() method does no longer change the fee of the original number.

## Example

Let’s take a seem at an instance of how to use the toString() technique in JavaScript.

For example:

```
var totn_number = 123.4;
console.log(totn_number.toString());
```

In this example, we have declared a variable called totn_number that is assigned the cost of 123.4. We have then invoked the toString() technique of the totn_number to convert the variety to a string representation of the number.

We have written the output of the toString() technique to the web browser console log, for demonstration purposes, to exhibit what the toString() method returns.

The following will be output to the net browser console log:

`123.4`

In this example, the output to the console log again “123.4” which is the string illustration of the quantity 123.4. Since there is no radix parameter provided, it displays the Decimal representation of the number.

Specifying a radix parameter

You can also specify a radix parameter to show the end result in a specific mathematical numeral machine than the Decimal system.

For example, let’s return our end result as a Binary number:

```
var totn_number = 13;
console.log(totn_number.toString(2));
console.log(totn_number.toString(8));
console.log(totn_number.toString(16));
```

The following will be output to the internet browser console log:

```
1101
15
d
```

In this example, the first output to the console log returned “1101”. Since we are using a radix of 2 to represent the Binary system, the number 13 will be transformed to the Binary cost of 1101 and again as the string fee “1101”.

The 2d output to the console log again “15”. A radix of 8 represents the Octal system, so the quantity thirteen will be transformed to Octal and returned as the string value “15”.

The 0.33 output to the console log back “d”. A radix of 16 represents the Hexadecimal system, so the variety 13 will be transformed to Hexadecimal and again as the string price “d”.

## Leave a Review