Mateen Kiani
Published on Sun Jul 06 2025·5 min read
How often have you found yourself juggling different data types in JavaScript? When working with numbers, you might focus on calculations, but converting those numbers into strings is just as important. That simple step can change how your app displays data, handles user input, or sends values over a network. Ever wondered which method to pick for the cleanest and most reliable result?
Let’s dive into the main ways to convert a number into a string in JavaScript. You’ll see how each approach works, and when to use it. By the end, you’ll know which method fits your code style, avoids bugs, and keeps your data flowing smoothly.
The most direct way to convert a number to a string is using the built-in toString()
method. It lives on every number you create. This method turns the number into a sequence of characters. You can also pass a radix (or base) to create binary, hex, or other formats.
let num = 255;let str = num.toString(); // "255"let hex = num.toString(16); // "ff"let bin = num.toString(2); // "11111111"
Notice how the optional argument changes the output. If you don’t pass any value, it defaults to base 10. A downside: if the value isn’t a number, calling toString()
might throw an error. Always be sure your variable is a number before using this method.
Tip: Use
Number.isFinite(value)
to check for valid numbers before conversion.
Another straightforward approach is the String()
function. It lives in the global scope and converts almost anything to a string. Even null
, undefined
, or objects get turned into a string. This broad handling makes it safer when you’re not 100% sure of your input type.
let val1 = String(123); // "123"let val2 = String(false); // "false"let val3 = String(null); // "null"
Under the hood, String()
calls the .toString()
method on most values. But if you pass null
or undefined
, it returns the words "null" or "undefined" instead of crashing. This makes it a good choice in user-facing code where you want to avoid unexpected exceptions.
Note:
String()
is slightly slower thantoString()
, but often the safety net is worth it.
Template literals offer a modern way to build strings. By wrapping backticks around your code, you can embed variables directly. This method is concise and works even if you mix numbers with text.
let score = 42;let message = `Your score is ${score}`; // "Your score is 42"
This approach shines when you need to include values in a larger sentence or structure. You avoid awkward plus signs and keep the code easy to read. It also automatically converts numbers, booleans, and other primitives to strings.
Pro Tip: Use template literals for multi-line strings or complex UI messages.
A quick and dirty way to convert a number is by adding an empty string + ''
. JavaScript will coerce the number into a string, and you get your result in one short line.
let count = 7;let text = count + ''; // "7"
This trick is popular for its brevity. But it can be confusing to readers who aren’t familiar with type coercion. Also, if you accidentally mix in other values, you might end up with unexpected results. Use it sparingly in tight loops or simple scripts.
Watch out:
0 + ''
returns "0" butnull + ''
returns "null".
Converting plain numbers is easy, but real-world data often brings surprises. Here are common edge cases to watch for:
console.log((NaN).toString()); // "NaN"console.log((Infinity).toString()); // "Infinity"console.log(String(undefined)); // "undefined"
When you see unexpected strings, validate your data first. If you need to go the other way—turning strings into numbers—check out how to convert strings to integers in JavaScript.
Best practice: Sanitize and validate before you convert.
If you care about speed, here’s a rough benchmark of common methods:
Method | Relative Speed |
---|---|
Concatenation (+'' ) | 100% |
toString() | 85% |
String() | 75% |
Template Literals | 70% |
// Example benchmark setup using console.time:console.time('concat'); count + ''; console.timeEnd('concat');console.time('toString'); count.toString(); console.timeEnd('toString');
For most apps, these differences are tiny. But in tight loops or heavy processing, the cheapest option can win. If you need to round numbers before converting, learn more about round to nearest integer.
Insight: Always profile in your real app, not just in isolation.
Converting numbers to strings in JavaScript is a basic skill every developer needs. You’ve seen four main options: toString()
, String()
, template literals, and concatenation. Each has its perks. toString()
is direct, String()
is safe, template literals excel in embedding, and concatenation is the fastest. When you face odd inputs like NaN
, Infinity
, or undefined
, validate first to avoid confusion.
Next time you build a chart label, log a value, or send data over HTTP, pick the method that matches your needs. Consider readability, performance, and error handling. With these tools in hand, you’ll write cleaner code and prevent common pitfalls in type conversion.
Use toString(), String(), template literals, or concatenation to reliably convert JavaScript numbers to strings and avoid unexpected results.