JavaScript toString() Method
Example
Converts a number to a string:
var
num
=
15
;
var
n
=
num
.
toString
(
)
;
n Output result:
15
Try it »
Definition and Usage
The string representation of a number. For example, when radix is 2, 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
number
.toString(
radix
)
Parameter value
| Parameters | Description |
|---|---|
| radix | Optional. Specifies the radix for representing the number, an integer between 2 and 36. If omitted, radix 10 is used. Note, however, that if this parameter is a value other than 10, the ECMAScript standard allows the implementation to return any value.
|
Return Value
| Type | Description |
|---|---|
| String | Convert a 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 radices:
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 result:
Try it »
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'
JavaScript Number object