JavaScript Convert Number to String

JavaScript Convert Number to String

Introduction

Managing data is one of the fundamental concepts of programming. Because of this, JavaScript offers plenty of tools to parse various data types, allowing you to easily interchange the format of data. Particularly, I'll be covering how to convert a Number to a String in this article. In another article I'll also be covering how to convert a String to an Number in JavaScript.

Comparing Data Types in JavaScript

JavaScript is a versatile language, which lets the programmer control how strict the data typing will be.

There are two main ways of comparing equality between two data structures/elements, two equal signs (==) or three equal signs (===).

When using two equal signs, the variables are compared only by its content. For this example, the ternary operator will be used, which is an if statement shortcut.

let a = 10;
let b = '10';

a == b ? console.log('Equal!') : console.log('Different!');

// Output:
//
// Equal!

On the other hand, when using three equal signs, the variables are compared by content and by data type:

let a = 50;
let b = '50';

a === b ? console.log('Equal!') : console.log('Different!');

// Output:
//
// Different!

Converting Number to String

There are some built-in methods in JavaScript that provide conversion from an number data type to a String, which only differ in performance and readability.

These are:

  • .toString()
  • String()
  • Template Strings
  • Concatenating an Empty String

.toString()

The .toString() method that belongs to the Number.prototype object, takes an integer or floating point number and converts it into a String type.

There are multiple ways of calling this method. If a number (base) is passed as a parameter to the .toString() method, the number will be parsed and converted to that base number:

let a = 20

a.toString();    // '20'
50 .toString();  // '50'
(60).toString(); // '60'
(7).toString(2); // '111' (7 in base 2, or binary)

String()

The String() method creates a primitive String type for the number that is passed to it:

let a = 30;

String(a);     // '30'
String(24);    // '24'
String(35.64); // '35.64'

The main difference here is that the String object does not do any base conversions like Number.toString() does.

Template Strings

With the introduction of template strings in ES6, injecting a number inside a String is a valid way of parsing an Integer or Float data type:

let num = 50;
let flt = 50.205;
let string = `${num}`;      // '50'
let floatString = `${flt}`; // '50.205'

Concatenating an Empty String

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Last, but not least, there is a nifty way of creating a String from a number. Arguably one of the most performance-driven way of doing so, even though some readability is compromised:

let a = '' + 50 // '50';

Depending on the browser this code runs on, it may be the fastest way of converting a number to a string. But also keep in mind that this method may not always return the desired string. For example:

let a = '' + 281e-26 // '2.81e-24'

Conclusion

There are many valid ways of manipulating data. It is up to the programmer to decide which one they prefer, choosing performance over readability or a balance between the two.

For more information, you can visit the following:

Last Updated: March 11th, 2019
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Getting Started with AWS in Node.js

    Build the foundation you'll need to provision, deploy, and run Node.js applications in the AWS cloud. Learn Lambda, EC2, S3, SQS, and more!

    © 2013-2021 Stack Abuse. All rights reserved.