Number toString() Method - Javascript Number

Javascript examples for Number:toString

Description

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

Syntax

number.toString(radix)

Parameter Values

Parameter Description
radix Optional. Must be an integer between 2 and 36.

radix value:

  • 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 Value:

A String, representing a number

The following code shows how to Convert a number to a string:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w  w  w  . j  a v  a  2s . c  o  m
    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 + "<br>" + b + "<br>" + c + "<br>" + d;

    document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

Related Tutorials