Back to JavaScript Fundamentals

How to Convert a Number to String in JavaScript - 5 Easy Methods

Learn multiple ways to convert numbers to strings in JavaScript, including toString(), String(), template literals and more. Complete guide with examples and best practices.
5 min readLogan FordLogan Ford
JavaScript code showing number to string conversion methods

Converting Numbers to Strings in JavaScript

Converting numbers to strings is a fundamental operation in JavaScript that you'll use frequently in web development. Whether you're formatting data for display, working with APIs, or manipulating text, knowing how to convert numbers to strings efficiently is essential. In this comprehensive guide, we'll explore multiple methods to perform this conversion, along with best practices and real-world examples.

Using the toString() Method

The toString() method is the most common way to convert a number to a string. Here's how to use it:

1const number = 42;
2const stringNumber = number.toString();
3console.log(stringNumber); // "42"
4console.log(typeof stringNumber); // "string"

The toString() method also accepts an optional radix parameter to convert numbers to different number systems:

1const number = 42;
2console.log(number.toString(2)); // "101010" (binary)
3console.log(number.toString(8)); // "52" (octal)
4console.log(number.toString(16)); // "2a" (hexadecimal)

The String() Function


The String() function provides a safer alternative, especially when dealing with null or undefined:

1const number = 123.456;
2const stringNumber = String(number);
3console.log(stringNumber); // "123.456"
4
5// Handles null and undefined safely
6console.log(String(null)); // "null"
7console.log(String(undefined)); // "undefined"

Template Literals

Modern JavaScript offers template literals for a more elegant way to convert numbers to strings:


1const number = 789;
2const stringNumber = `${number}`;
3console.log(stringNumber); // "789"
4
5// Useful for formatting
6const price = 49.99;
7const message = `The item costs $${price}`;
8console.log(message); // "The item costs $49.99"

String Concatenation

You can also convert numbers to strings by concatenating them with an empty string:

1const number = 12345;
2const stringNumber = number + '';
3console.log(stringNumber); // "12345"
4console.log(typeof stringNumber); // "string"

Best Practices and Performance

When choosing a conversion method, consider these factors:

  1. Use toString() for basic number conversions
  2. Use String() when dealing with null/undefined values
  3. Use template literals for string interpolation
  4. Avoid string concatenation for conversion (it's less readable)
1// ✅ Good Practice
2const number = 42;
3const formatted = number.toString();
4
5// ✅ Safe Practice
6const value = null;
7const safeString = String(value);
8
9// ✅ Modern Practice
10const price = 99.99;
11const display = `Price: $${price}`;
12
13// ❌ Avoid
14const num = 123;
15const badPractice = num + '';

Common Use Cases

Here are some practical examples where number-to-string conversion is useful:

1// Formatting phone numbers
2const phone = 1234567890;
3const formatted = phone.toString().replace(/(d{3})(d{3})(d{4})/, '($1) $2-$3');
4console.log(formatted); // "(123) 456-7890"
5
6// Currency formatting
7const price = 1234.56;
8const currency = price.toString().replace(/B(?=(d{3})+(?!d))/g, ',');
9console.log(currency); // "1,234.56"

Converting numbers to strings is a fundamental skill in JavaScript. By understanding these different methods and their appropriate use cases, you can write more efficient and maintainable code. Remember to consider the context and requirements of your specific situation when choosing a conversion method.


Want to practice these concepts? Try our interactive JavaScript challenges and put your knowledge to the test!

Land your dream tech job faster

Join 810+ developers who are accelerating their coding skills with TechBlitz.

Share this article