{"id":476,"date":"2023-11-16T15:06:49","date_gmt":"2023-11-16T15:06:49","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=476"},"modified":"2023-11-23T08:25:12","modified_gmt":"2023-11-23T08:25:12","slug":"number-to-string-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/number-to-string-javascript\/","title":{"rendered":"Convert Number to String in JavaScript (5 Easy Methods)"},"content":{"rendered":"\n<p>Whether you want to display a number as part of a sentence or pass it as a string to a function or API, there is always a need to conversion of number to string. In this article, we will learn about various methods to convert a  Number to a String in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Strings in JavaScript?<\/strong><\/h2>\n\n\n\n<p><strong>Strings in JavaScript represent a sequence of multiple characters.<\/strong> \u2018C\u2019 represents a single character and \u201cCat\u201d represents a string. In JavaScript, we can declare a string in two ways. Let us look into each method.<\/p>\n\n\n\n<p>We can declare a string in JavaScript by making a string literal using double quotes. The syntax and example for the same is given below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet a=&quot;FavTutor&quot;\n\n\/\/printing the string\nconsole.log(a);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>We can also declare a string in JavaScript by creating a String object:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string using String Object\nlet str=new String(&quot;FavTutor&quot;)\n\n\/\/printing the string\nconsole.log(str);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; String: \u2018FavTutor\u2019 ]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5 Methods to Convert Number to String\u00a0in JavaScript<\/strong><\/h2>\n\n\n\n<p>In this section, we will look at various techniques that help us convert a JavaScript Number into a String. Let us begin!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using toString() Function<\/strong><\/h3>\n\n\n\n<p><strong>One of the easiest and most concise methods of converting a number to a string is by using the toString() function.<\/strong> <\/p>\n\n\n\n<p>The syntax for the toString() method is given as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let convertedStr = num.toString(radix)<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>convertedStr:<\/strong> variable to store the string that we get from the toString() function.<\/p>\n\n\n\n<p><strong>num:<\/strong> variable representing the number that has to be converted to string<\/p>\n\n\n\n<p><strong>radix:<\/strong> optional parameter, can be 2(binary), 8(octal), 10(decimal), or 16(hexadecimal) according to the requirements<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a number\nlet num=9;\n\n\/\/printing and checking the type of num variable\nconsole.log(num + &quot; &quot; + typeof(num));\n\n\/\/using the toString() method with different radices\nlet convertedStrBin = num.toString(2);\nlet convertedStrOct = num.toString(8);\nlet convertedStrHex = num.toString(16);\nlet convertedStrDec = num.toString(10);\n\n\/\/printing and checking type of convertedStr variables\nconsole.log(convertedStrBin + &quot; &quot; + typeof(convertedStrBin));\nconsole.log(convertedStrOct + &quot; &quot; + typeof(convertedStrOct));\nconsole.log(convertedStrDec + &quot; &quot; + typeof(convertedStrDec));\nconsole.log(convertedStrHex + &quot; &quot; + typeof(convertedStrHex));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9 number\n1001 string\n11 string\n9 string\n9 string<\/code><\/pre>\n\n\n\n<p>In this example, we have used the toString() function to convert the number 9 to a string. We passed various radices and stored the obtained strings in different variables which are printed on the console. We have obtained the desired result.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using String() constructor<\/strong><\/h3>\n\n\n\n<p>String() is a constructor of the class String. It takes any value or variable as the parameter and converts it into a string.&nbsp;<\/p>\n\n\n\n<p><strong>The difference between the toString() function and the String() constructor is that the toString() method can convert the number into different decimal forms and then to a string, i.e. it takes radix as an optional parameter. The String() parameter would simply convert the number to a string in whichever form it is passed.\u00a0<\/strong><\/p>\n\n\n\n<p>Let us understand with the help of an example.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a number\nlet num=9;\n\n\/\/printing and checking the type of num variable\nconsole.log(num + &quot; &quot; + typeof(num));\n\n\/\/using the String() Constructor\nlet convertedStr = String(num);\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9 number\n9 string\n<\/code><\/pre>\n\n\n\n<p>As shown in the example and the output, the number 9 is converted into the string 9 with the help of the String() constructor.<\/p>\n\n\n\n<p>Let us try converting float into a string:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a float number\nlet num=9.6;\n\n\/\/printing and checking the type of num variable\nconsole.log(num + &quot; &quot; + typeof(num));\n\n\/\/using the String() Constructor\nlet convertedStr = String(num);\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9.6 number\n9.6 string<\/code><\/pre>\n\n\n\n<p>This method works with float numbers as well. We have obtained the desired result<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Concatenating the Empty String<\/strong><\/h3>\n\n\n\n<p>Concatenating an empty string with a number is a simple and efficient way for conversion from number to string. This method involves using the \u201c+\u201d operator to concatenate the number with an empty string. Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a number\nlet num=9;\n\n\/\/printing and checking the type of num variable\nconsole.log(num + &quot; &quot; + typeof(num));\n\n\/\/using the &quot;+&quot; operator\nlet convertedStr = &quot;&quot; + num;\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9 number\n9 string<\/code><\/pre>\n\n\n\n<p>We have obtained the desired output. Let us try the same with floating point numbers:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a Float number\nlet numFloat=9.6;\n\n\/\/printing and checking the type of numFloat variable\nconsole.log(numFloat + &quot; &quot; + typeof(numFloat));\n\n\/\/using the &quot;+&quot; operator\nlet convertedStr = &quot;&quot; + numFloat;\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9.6 number\n9.6 string\n<\/code><\/pre>\n\n\n\n<p>The method works with both integers and floating-point numbers.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Leveraging Template Strings<\/strong><\/h3>\n\n\n\n<p>With the introduction of template strings in ES6, one can inject a number into a string using backticks (`). This approach is called string interpolation. It provides a concise and readable way to convert numbers to strings.<\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a number\nlet num=9;\n\n\/\/printing and checking the type of num variable\nconsole.log(num + &quot; &quot; + typeof(num));\n\n\/\/using the template strings\nlet convertedStr = `${num}`\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9 number\n9 string\n<\/code><\/pre>\n\n\n\n<p>We have obtained the desired output. Let us try the same with floating point numbers.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a Float number\nlet numFloat=9.6;\n\n\/\/printing and checking the type of numFloat variable\nconsole.log(numFloat + &quot; &quot; + typeof(numFloat));\n\n\/\/using the template strings\nlet convertedStr =`${numFloat}`;\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9.6 number\n9.6 string\n<\/code><\/pre>\n\n\n\n<p>The method works with both integers and floating-point numbers.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5) Using the toFixed() Method<\/strong><\/h3>\n\n\n\n<p>The toFixed() method is another method of converting a number to a string. But, this method is primarily used when you need to convert a number to a string with a specific number of decimal places. This method rounds the number to the specified precision and returns it as a string.<\/p>\n\n\n\n<p>Syntax of toFixed() Function is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let convertedStr = num.toFixed(decimal_places)<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>convertedStr:<\/strong> variable to store the string that we get from the toFixed() function.<\/p>\n\n\n\n<p><strong>num: <\/strong>variable representing the number that has to be converted to string<\/p>\n\n\n\n<p><strong>decimal_places<\/strong>: a number that represents the decimal places by which the floating point number has to be rounded.<\/p>\n\n\n\n<p>Let us take an example.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a Float number\nlet numFloat=9.6743;\n\n\/\/printing and checking the type of numFloat variable\nconsole.log(numFloat + &quot; &quot; + typeof(numFloat));\n\n\/\/using the toFixed() function\nlet convertedStr = numFloat.toFixed(2);\n\n\/\/printing and checking type of convertedStr variable\nconsole.log(convertedStr + &quot; &quot; + typeof(convertedStr));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>9.6743 number\n9.67 string\n<\/code><\/pre>\n\n\n\n<p>We have used the toFixed() method to convert a floating point number to a string. We rounded down the floating point number to 2 decimal places by passing 2 as a parameter in the toFixed() function and finally stored the string obtained from the function in a variable.<\/p>\n\n\n\n<p><strong>Note that toFixed() returns a string representation of the number, not a rounded number value. <\/strong>We have obtained the desired result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Choosing the Right Method<\/strong><\/h2>\n\n\n\n<p>We have to consider the specific requirements of a project while choosing a method for conversion.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Performance:<\/strong> Concatenation with an empty string and template strings tend to be faster and hence show better performance.<br><\/li>\n\n\n\n<li><strong>Readability:<\/strong> String interpolation and concatenation with an empty string are highly readable and make the code more expressive.<br><\/li>\n\n\n\n<li><strong>Decimal Precision:<\/strong> If you need to control the number of decimal places, the toFixed() method is a suitable choice.<br><\/li>\n<\/ul>\n\n\n\n<p>One can choose the method that best suits the needs according to these factors. You can now check t another tutorial if you are curious about how to <a href=\"https:\/\/favtutor.com\/articles\/convert-string-to-number\/\">convert string to number<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we discussed various techniques to convert a number to a string in JavaScript. It is essential to know at least three of them when working in JavaScript as they sometimes come in very handy, enhancing the flexibility and usability of your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about various methods for converting Number to String in JavaScript with code. We will use toString() function, Template Strings etc.<\/p>\n","protected":false},"author":9,"featured_media":478,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,44],"class_list":["post-476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-string"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=476"}],"version-history":[{"count":7,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":534,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/476\/revisions\/534"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/478"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}