Easy Tutorial
❮ Jsref Repeat Prop Img Align ❯

JavaScript toString() Method

JavaScript Number Object

Example

Convert a number to a string:

var num = 15;
var n = num.toString();

n output result:


Definition and Usage

The string representation of a number. For example, when radix is 2, the NumberObject is converted to a string representing its binary value.


Browser Support

Method
toString() Yes Yes Yes Yes Yes

All major browsers support the toString() method.


Syntax

Parameter Values

Parameter Description
radix Optional. Specifies the base (radix) in which the number is represented, an integer between 2 and 36. If omitted, base 10 is used. Note that if the value is other than 10, the ECMAScript standard allows implementations to return arbitrary values. 2 - Number displayed in binary<br> 8 - Number displayed in octal<br> 16 - Number displayed in hexadecimal

Return Value

Type Description
String Converts the number to a string

Technical Details

| JavaScript Version: | 1.1 | | --- | --- |


More Examples

Example

In this example, we convert a number to a string using different bases:

var num = 15;
var a = num.toString();
var b = num.toString(2);
var c = num.toString(8);
var d = num.toString(16);

a, b, c, and d output results:

var num = new Number(15);
document.write(num.toString() + "<br>");
document.write(num.toString(2) + "<br>");
document.write(num.toString(8) + "<br>");
document.write(num.toString(16) + "<br>");

More examples:

var count = 10;

console.log(count.toString());    // Outputs '10'
console.log((17).toString());     // Outputs '17'
console.log((17.2).toString());   // Outputs '17.2'

var x = 6;

console.log(x.toString(2));       // Outputs '110'
console.log((254).toString(16));  // Outputs 'fe'

console.log((-10).toString(2));   // Outputs '-1010'
console.log((-0xff).toString(2)); // Outputs '-11111111'

JavaScript Number Object

❮ Jsref Repeat Prop Img Align ❯