JavaScript Number toString() Method | String to number js
ExamplesConvertanumbertoastring:letnum=15;lettext=num.toString();TryitYourself»[1]Convertanumbertoastring,usingbase2(binary):letnum=15;lettext=num.toString(2);TryitYourself»[2]DescriptionThetoString()returnsanumberasastring.NoteEveryJavaScriptobjecthasatoString()method.ThetoString()methodisusedinternallybyJavaScriptwhenanobjectneedstobedisplayedasatext(likeinHTML),orwhenanobjectneedstobeusedasastring.Normally,youwillnotuseitinyourowncode.SyntaxParametersParameterDescriptionradixOptional.Theb...
ExamplesConvert a number to a string:
let num = 15;let text = num.toString();
Try it Yourself »[1]Convert a number to a string, using base 2 (binary):
let num = 15;let text = num.toString(2);
Try it Yourself »[2] DescriptionThe toString() returns a number as a string.
NoteEvery JavaScript object has a toString() method.
The toString() method is used internally by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string.
Normally, you will not use it in your own code.
Syntax Parameters Parameter Description radix Optional.The base to use.Must be an integer between 2 and 36.Base 2 is binaryBase 8 is octalBase 16 is hexadecimal. Return Value Type Description A stringThe number as a string. More ExamplesConvert a number to a string, using base 8 (Octal):
let num = 15;let text = num.toString(8);
Try it Yourself »[3]