{"id":539,"date":"2023-11-24T04:51:57","date_gmt":"2023-11-24T04:51:57","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=539"},"modified":"2023-11-25T08:42:00","modified_gmt":"2023-11-25T08:42:00","slug":"convert-array-to-string-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/convert-array-to-string-javascript\/","title":{"rendered":"Convert Array to String JavaScript (5 Easy Methods)"},"content":{"rendered":"\n<p>JavaScript is a versatile programming language that offers various methods to manipulate data structures. One common task that you will often encounter is converting something to a string. In this article, we will take a deep dive into various techniques to convert array to string in JavaScript. Before that, let&#8217;s revise what arrays and strings are really!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Arrays and Strings in JavaScript?<\/strong><\/h2>\n\n\n\n<p><strong>Arrays are linear data structures that store homogenous data, i.e. same type of data.<\/strong> An array can contain all integers, all characters, or all strings as the elements. An array can not be a combination of integers and characters, or integers or strings. Let us look at how to declare and use arrays in Javascript.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>\/\/declaring an integers array\nlet marks = &#91;98, 75, 62, 82, 97, 53];\n\n\/\/declaring a characters array\nlet grades=&#91;'A', 'B', 'A', '0'];\n\n\/\/declaring a strings array\nlet favtutors=&#91;\"John\", \"Joshua\", \"Alex\", \"Kate\"];\n\n\/\/declaring a boolean array\nlet present=&#91;true, false, false, true];\n<\/code><\/pre>\n\n\n\n<p>In the code above, we have made integers array marks containing all integers, a characters array grade consisting of all elements as characters, a strings array favtutors consisting of all strings, and a boolean present array consisting of all boolean values.<\/p>\n\n\n\n<p>Strings in JavaScript represent a sequence of multiple characters. \u2018C\u2019 represents a single character and \u201cCat\u201d represents a string. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5 Methods to Convert Array to String in JavaScript<\/strong><\/h2>\n\n\n\n<p>In this section, we will discuss various techniques on how to convert an array to a string in JavaScript.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the toString() Method<\/strong><\/h3>\n\n\n\n<p>The simplest method to convert array to string in JavaScript is by using the toString() method. <strong>The toString() method converts each element of the array into a string and concatenates them together, separated by commas.&nbsp;<\/strong><\/p>\n\n\n\n<p>The syntax 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 = arr.toString()<\/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>arr:<\/strong> variable representing the array that has to be converted to a string.<\/p>\n\n\n\n<p>Let us look into 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 arrays\nlet arrNum=[1,2,3,4];\n\n\/\/declaring a char array\nlet arrChar=['f','a','v'];\n\n\/\/printing and checking the type of arrays\nconsole.log(arrNum + &quot; &quot; + typeof(arrNum));\nconsole.log(arrChar + &quot; &quot; + typeof(arrChar));\n\n\/\/using the toString() function\nlet convertedStrNum = arrNum.toString();\nlet convertedStrChar = arrChar.toString();\n\n\/\/printing and checking type of convertedStr variables\nconsole.log(convertedStrNum + &quot; &quot; + typeof(convertedStrNum));\nconsole.log(convertedStrChar + &quot; &quot; + typeof(convertedStrChar));<\/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>1,2,3,4 object\nf,a,v object\n1,2,3,4 string \nf,a,v string\n<\/code><\/pre>\n\n\n\n<p>In this example, we have taken two arrays, one array of integer numbers and the other of characters. We pass these two arrays as parameters to the toString() method that converts them into strings. We store the strings in the variables and print them on the console. We have received the desired output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using the join() Method<\/strong><\/h3>\n\n\n\n<p>The join() method is another method to convert an array to a string in JavaScript. We can choose a specific custom separator to join the elements of the array. The comma(,) is the default separator. Let&#8217;s explore how to use the join() method.<\/p>\n\n\n\n<p>The syntax of the join() method 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 = arr.join(separator)<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>convertedStr:<\/strong> variable to store the string from the toString() function.<\/p>\n\n\n\n<p><strong>arr:<\/strong> variable representing the array that has to be converted to a string<\/p>\n\n\n\n<p><strong>Note that separator is an optional parameter here, by default, it\u2019s a comma(,).<\/strong><\/p>\n\n\n\n<p>Here\u2019s an example of the same.<\/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 array\nlet arrNum=[1,2,3,4];\nlet arrString=['fav','tutor'];\n\n\/\/printing and checking the type of arrays\nconsole.log(arrNum + &quot; &quot; + typeof(arrNum));\nconsole.log(arrString + &quot; &quot; + typeof(arrString));\n\n\/\/using the join() function\nlet convertedStrNum = arrNum.join(',');\nlet convertedStr = arrString.join('');\n\n\/\/printing and checking type of convertedStr variables\nconsole.log(convertedStrNum + &quot; &quot; + typeof(convertedStrNum));\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>1,2,3,4 object\nfav,tutor object\n1,2,3,4 string\nfavtutor string\n<\/code><\/pre>\n\n\n\n<p>In this example, we have taken two arrays, one integer array, and one string array. We have used the join() method to convert the two arrays to strings.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For the integer array, we used a comma as the separator.<\/li>\n\n\n\n<li>For the string array, we used the separator as an empty string (\u2018\u2019)<\/li>\n<\/ul>\n\n\n\n<p>We have obtained the desired results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using JSON.stringify() Method<\/strong><\/h3>\n\n\n\n<p>There are times when you need to convert array to string without losing the array structure. <strong>JavaScript provides the JSON.stringify() method, which converts JavaScript objects, including arrays, to JSON strings.<\/strong> This method can be used to convert arrays to strings in a structured format.\u00a0<\/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 = JSON.stringify(arr);<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>convertedStr:<\/strong> variable to store the string from the toString() function.<\/p>\n\n\n\n<p><strong>arr:<\/strong> variable representing the array that has to be converted to a string<\/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 array\nlet arrNum=[1,2,3,4];\nlet arrString=['fav','tutor'];\n\n\/\/printing and checking the type of arrays\nconsole.log(arrNum + &quot; &quot; + typeof(arrNum));\nconsole.log(arrString + &quot; &quot; + typeof(arrString));\n\n\/\/using the json.stringify() function\nlet convertedStrNum = JSON.stringify(arrNum)\nlet convertedStr = JSON.stringify(arrString);\n\n\/\/printing and checking type of convertedStr variables\nconsole.log(convertedStrNum + &quot; &quot; + typeof(convertedStrNum));\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>1,2,3,4 object\nfav,tutor object\n&#91;1,2,3,4] string\n&#91;\u201cfav\u201d,\u201dtutor\u201d] string\n<\/code><\/pre>\n\n\n\n<p>We can see the stringified strings with the enclosed array brackets. Hence, the JSON.stringify method <strong>instead of<\/strong> joining all elements of an array and converting them to a string, makes the whole of an array along with the structure, a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Using the Concatenation (+) Operator<\/strong><\/h3>\n\n\n\n<p>This method is also called Implicit coercion. It automatically converts the data types from one to another. <strong>In the case of converting an array to a string, JavaScript implicitly coerces the array to a string using the concatenation operator (+).&nbsp;<\/strong><\/p>\n\n\n\n<p>Let us take an example to understand this better:<\/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 array\nlet arrNum=[1,2,3,4];\nlet arrString=['fav','tutor'];\n\n\/\/printing and checking the type of arrays\nconsole.log(arrNum + &quot; &quot; + typeof(arrNum));\nconsole.log(arrString + &quot; &quot; + typeof(arrString));\n\n\/\/using the + operator\nlet convertedStrNum = &quot;&quot; + arrNum\nlet convertedStr = &quot;&quot; + arrString;\n\n\/\/printing and checking type of convertedStr variables\nconsole.log(convertedStrNum + &quot; &quot; + typeof(convertedStrNum));\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>1,2,3,4 object\nfav,tutor object\n1,2,3,4 string\nfav,tutor string\n<\/code><\/pre>\n\n\n\n<p>We have obtained the desired output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5) Using the String() Constructor<\/strong><\/h3>\n\n\n\n<p>Explicit coercion is the manual conversion of data types using built-in JavaScript functions. The String() function of the class String can be used to explicitly coerce an array to string.<\/p>\n\n\n\n<p>The syntax of the String() method 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 = String(arr)<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>convertedStr:<\/strong> variable to store the string from the toString() function.<\/p>\n\n\n\n<p><strong>arr:<\/strong> variable representing the array that has to be converted to a string<\/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 array\nlet arrNum=[1,2,3,4];\nlet arrString=['fav','tutor'];\n\n\/\/printing and checking the type of arrays\nconsole.log(arrNum + &quot; &quot; + typeof(arrNum));\nconsole.log(arrString + &quot; &quot; + typeof(arrString));\n\n\/\/using the String() method\nlet convertedStrNum = String(arrNum)\nlet convertedStr = String(arrString);\n\n\/\/printing and checking type of convertedStr variables\nconsole.log(convertedStrNum + &quot; &quot; + typeof(convertedStrNum));\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>1,2,3,4 object\nfav,tutor object\n1,2,3,4 string\nfav,tutor string\n<\/code><\/pre>\n\n\n\n<p>We have used the String() method to convert the array to string. We have obtained the desired output.<\/p>\n\n\n\n<p>You can check out this article if you want to learn how to <a href=\"https:\/\/favtutor.com\/articles\/string-to-array-javascript\/\">Convert a String to an Array in JavaScript.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have discussed the various techniques to convert an to a string in JavaScript. It is important to keep at least 3 methods in mind as they come in very handy at times. It is important to understand and consider the best practices that help choose an appropriate method based on the requirements. Also learn, how to <a href=\"https:\/\/favtutor.com\/articles\/set-to-array-javascript\/\">convert a set to array in JavaScript.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to convert Array to String in JavaScript using the toString() method, join() function, concatenation operator, etc with code.<\/p>\n","protected":false},"author":9,"featured_media":542,"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,13,38],"class_list":["post-539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-array","tag-javascript-strings"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/539","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=539"}],"version-history":[{"count":6,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"predecessor-version":[{"id":584,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/539\/revisions\/584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/542"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}