How to convert Number to string in Javascript

Description

toString() method optionally accepts a single argument indicating the radix:

  • string toString()
    Represents a number in base 10
  • string toString(2)
    Represents a number in base 2(binary)
  • string toString(8)
    Represents a number in base 8(octal)
  • string toString(16)
    Represents a number in base 16(hexadecimal)

Example


var num = 10; 
//w  ww  .  j  a v a  2  s  .c o m
console.log(num.toString()); //"10" 
console.log(num.toString(2)); //"1010" 
console.log(num.toString(8)); //"12" 
console.log(num.toString(10)); //"10" 
console.log(num.toString(16)); //"a" 

The code above generates the following result.





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window