Javascript - Number toString() Method

The toString() method converts a number to a string.

Description

The toString() method converts a number to a string.

Syntax

number.toString(radix)

Parameter Values

ParameterRequire Description
radixOptional. base to use for representing a numeric value.

The radix Must be an integer between 2 and 36.

  • 2 - The number will show as a binary value
  • 8 - The number will show as an octal value
  • 16 - The number will show as an hexadecimal value

Return

A String, representing a number

Example

Convert a number to a string:

Demo

//display the formatted number.
var num = 15;/*from   ww w .j a  v  a 2 s  . co m*/
var n = num.toString();
console.log(n);

//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);

var n = a + "\n" + b + "\n" + c + "\n" + d;

console.log(n);

Result