Ship internal tools in hours, not weeks. Real auth, users, jobs, audit logs, and cohesive UI included. Early access $249 $499 → [Get it now]

How to convert a number to a string in JavaScript

Converting numbers to strings is essential for display formatting, API data preparation, URL building, and text concatenation in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve used number-to-string conversion extensively in display components, form fields, and data export features. For the reverse operation, see how to convert a string to a number in JavaScript.

Use toString() or String() to convert a number to its string representation.

(42).toString()   // '42'
String(42)        // '42'
`${42}`           // '42'

1. toString()

toString() is the most explicit method. Call it on a number variable or wrap a literal in parentheses to avoid a syntax error.

const n = 3.14159
n.toString()           // '3.14159'

const int = 255
int.toString()         // '255'

// Literal syntax — parentheses required
(42).toString()        // '42'
// 42.toString()       // SyntaxError — JS parser reads the dot as decimal point

toString() throws a TypeError if called on null or undefined — use String() instead when the value may be nullish.

2. String() — Safe for Null and Undefined

String() used as a function (not new String()) is the safest general-purpose conversion. It never throws.

String(42)          // '42'
String(3.14)        // '3.14'
String(NaN)         // 'NaN'
String(Infinity)    // 'Infinity'
String(-Infinity)   // '-Infinity'
String(null)        // 'null'      — safe, no throw
String(undefined)   // 'undefined' — safe, no throw

Use String() at system boundaries — API responses, user input processing, or anywhere the value might be null or undefined.

3. Template Literals

Template literals implicitly call toString() on embedded values and are the most readable option when building strings with embedded numbers.

const price = 9.99
const label = `Price: $${price}`           // 'Price: $9.99'

const page = 3
const url = `/products?page=${page}`       // '/products?page=3'

const count = 42
const message = `Found ${count} results`  // 'Found 42 results'

Prefer template literals for string composition. Use toString() or String() when you need just the converted value.

4. Base Conversion with Radix

toString(radix) converts a number to a string in a different base — binary, octal, hex, and any base from 2 to 36.

(255).toString(16)   // 'ff'    — hexadecimal
(255).toString(2)    // '11111111' — binary
(255).toString(8)    // '377'   — octal
(255).toString(10)   // '255'   — decimal (default)
(36).toString(36)    // '10'    — base-36

// Useful for generating short IDs
Math.random().toString(36).slice(2, 8)  // e.g. 'k7x2q1'

Note: toString(radix) produces a lowercase string for hex. Use .toUpperCase() if you need 'FF' instead of 'ff'.

5. Controlling Decimal Places

When converting a float for display, control how many decimal digits appear.

const pi = 3.14159265

pi.toFixed(2)         // '3.14'  — string, rounds to 2 decimals
pi.toPrecision(4)     // '3.142' — string, 4 significant digits
pi.toExponential(2)   // '3.14e+0' — scientific notation

// Parse back to number if needed
parseFloat(pi.toFixed(2))  // 3.14

These methods always return strings, so they combine conversion and formatting in one step — handy for price displays, sensor readings, and chart labels.

6. Choosing the Right Method

Method Null-safe Radix Precision Use case
n.toString() no yes no Explicit conversion, base conversion
String(n) yes no no Safe conversion at boundaries
`${n}` yes no no String interpolation
n.toFixed(d) no no decimal Display with fixed decimals
n.toPrecision(d) no no significant Significant figures

Best Practice Note:

Use String(n) when the value may be null or undefined — it is the safest default. Use toString(radix) for base conversion and template literals for string interpolation. Avoid new String(n) — it creates a String object, not a primitive, which causes subtle equality bugs (new String('42') !== '42').


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
Passing props to child components in React function components
Passing props to child components in React function components

How to return multiple values from a JavaScript function
How to return multiple values from a JavaScript function

How to force a React component to re-render
How to force a React component to re-render

How to Remove Elements from a JavaScript Array
How to Remove Elements from a JavaScript Array

Answers by CoreUI Core Team