JavaScript toString()Method

Number 对象参考手册JavaScript Number Object

Example

Convert the number to a string:

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

nOutput:

15

Try it Yourself »

Definition and Usage

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


Browser Support

Method
toString() Yes Yes Yes Yes Yes

All major browsers support the toString() method.


Syntax

number.toString(radix)

Parameter Values

Parameter Description
radix Optional. Specifies the radix used to represent the number, an integer between 2 and 36. If this parameter is omitted, radix 10 is used. However, note that if this parameter is a value other than 10, the ECMAScript standard allows implementations to return any arbitrary value.
  • 2 - The number is shown as a binary value
  • 8 - The number is shown as an octal value
  • 16 - The number is shown as a hexadecimal value

Return Value

Type Description
String Converts a number to a string

Technical Details

JavaScript Version: 1.1


More Examples

Example

In this example, we use different radices to convert a number to a string:

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

a、b、canddOutput:


Try it Yourself »

More Examples:

var count = 10;

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

var x = 6;

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

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


Number 对象参考手册JavaScript Number Object

Other Extensions