Number.prototype.toString
Returns a string representing the specified Number object in the specified radix (base)
Syntax
number.toString(radix?)Parameters
| Parameter | Type | Description |
|---|---|---|
| radix | number | An integer between 2 and 36 specifying the base |
Return Value
A string representing the specified number in the given radix
Examples
console.log((255).toString(16)); // 'ff'
console.log((255).toString(2)); // '11111111'console.log((10).toString()); // '10'
console.log((10).toString(8)); // '12'function toHex(num: number) {
return '0x' + num.toString(16).toUpperCase();
}
console.log(toHex(255)); // '0xFF'Understanding Number.prototype.toString
The Number.prototype.toString method in JavaScript returns a string representing the specified Number object in the specified radix (base). It belongs to the Number object and is one of the most widely used methods for working with number values in modern JavaScript and TypeScript applications.
The method signature is number.toString(radix?). It accepts 1 parameter: radix. When called, it returns a string representing the specified number in the given radix. Understanding when and how to use toString() helps you write more expressive, readable code.
Common use cases for Number.prototype.toString include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like number-parseint, number-tofixed, string-charat, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Number.prototype.toString is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Number Methods
Other methods in the Number object
Related Tools
More Number Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.