JavaScript: int to string

Writer: Ryan Frankel

Ryan Frankel, CTO and Contributing Expert

Ryan began developing websites in the late '90s and has personally tested just about every web host and cloud platform worth trying on the market today. With a masters degree in electrical and computer engineering from the University of Florida, he leverages his extensive knowledge of hardware, software, and their engineering relationship to inform HostingAdvice readers of the technical implications of their hosting choices. Ryan's subject matter expertise includes, but is not limited to, WordPress, cloud infrastructure management, product UI/UX design, and popular web development languages such as JavaScript and PHP.

Editor: Lillian Castro

Lillian Castro, Senior Editor

Lillian Castro brings more than 30 years of editing and journalism experience to our team. She has written and edited for major news organizations, including The Atlanta Journal-Constitution and the New York Times, and she previously served as an adjunct instructor at the University of Florida. Today, she edits HostingAdvice content for clarity, accuracy, and reader engagement.

Reviewer: Cristian Lopez

Cristian Lopez, News Manager

Cristian Lopez uses his Business Marketing background from the University of Illinois at Chicago to create comfortable environments for customers, clients, and colleagues to share their thoughts and ideas openly. From interviewing tech leaders to conducting UX market research projects, Cristian knows the importance of storytelling — a key variable for innovation and inspiration. His goal at HostingAdvice is to wow readers on the ever-evolving nature of the tech industry and bring his audience the most reliable and exciting content on all things hosting.

Disclosure: When you purchase through links on our site, we often earn referral fees from partners. For more information, see our ad disclosure and review policy.

Follow Us:
2.7k
16k
5.7k
134
3.5k

Every built-in JavaScript object type has a toString() method. This method is automatically called when the object needs to be represented as a string value (i.e., when you concatenate some string value with a number), or it can be called manually when such a type conversion is wanted.

The toString() method works on all of the standard JS objects: arrays, dates, numbers, errors, et cetera. For number types, the toString() method also can take an optional “radix” argument: number.toString(radix)

The radix (or base) is the total number of unique digits, including zero, that are used to represent a particular number system. For number systems other than the decimal system, you can use the radix argument to correctly stringify a number from some other number system, such as the octal (base 8) and hexadecimal (base 16) number systems.

Here are some examples of the toString() method being used on numbers:

var x = 10; // integer number  var y = 7.950; // floating point number  var z = 1.21e–6; // floating point number in a scientific notation  var inf = Number.POSITIVE_INFINITY; // infinity    console.log(‘hello’ + x); // 'hello10' - toString() is called automatically  console.log(x.toString()); // “10” - decimal base  console.log(x.toString(2)); // “1010” - binary base  console.log(x.toString(8)); // “12” - octal base  console.log(x.toString(16)); // “a” - hex base    console.log(y.toString()); // “7.95” - note the stripped “0” at the end  console.log(y.toString(2)); // “111.11110011001100111000011100010110000010010101011011”  console.log(y.toString(16)); // “7.f33387160956c”    console.log(z.toString()); // “0.00000121”    console.log(inf.toString()); // “Infinity”  console.log(inf.toString(2)); // “Infinity”

There isn’t really much more to using toString() on numbers in JavaScript. As always, if you have any questions, ask away in a comment below.

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.

About the Author

CTO and Contributing Expert

Ryan Frankel has been a professional in the tech industry for more than 20 years and has been developing websites for more than 25. With a master's degree in electrical and computer engineering from the University of Florida, he has a fundamental understanding of hardware systems and the software that runs them. Ryan now sits as the CTO of Digital Brands Inc. and manages all of the server infrastructure of their websites, as well as their development team. In addition, Ryan has a passion for guitars, good coffee, and puppies.

« BACK TO: HOW-TO

Meet the Experts

Our team of experts with a combined 50+ years of experience in web hosting serve insight and advice to more than 20 million users!

We Know Hosting

$

4

8

,

2

8

3

spent annually on web hosting!