{"id":1040,"date":"2026-11-18T06:19:09","date_gmt":"2026-11-18T06:19:09","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1040"},"modified":"2026-11-18T06:19:09","modified_gmt":"2026-11-18T06:19:09","slug":"javascript-array-to-string","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-array-to-string\/","title":{"rendered":"JavaScript Array to String: toString(), join() and More"},"content":{"rendered":"<p>Hello friends! Converting an array to a string is something you do all the time in JavaScript: showing a list of names on a page, building a sentence, saving data or sending it to a server. There are several ways to do it, and each gives a slightly different result. In this lesson, let&#8217;s compare them one by one, with real outputs, so you know exactly which one to use when.<\/p>\n<h2>1. toString(): Comma-Separated, No Spaces<\/h2>\n<pre class=\"wp-block-code\"><code>const fruits = [\"apple\", \"mango\", \"banana\"];\nconsole.log(fruits.toString());<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>apple,mango,banana<\/code><\/pre>\n<p><code>toString()<\/code> always uses a comma with no space. You can&#8217;t change the separator.<\/p>\n<h2>2. join(): Choose Your Own Separator<\/h2>\n<pre class=\"wp-block-code\"><code>console.log(fruits.join());        \/\/ default is a comma\nconsole.log(fruits.join(\", \"));\nconsole.log(fruits.join(\" | \"));\nconsole.log(fruits.join(\"\"));      \/\/ no separator\nconsole.log(fruits.join(\"n\"));    \/\/ one item per line<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>apple,mango,banana\napple, mango, banana\napple | mango | banana\napplemangobanana\napple\nmango\nbanana<\/code><\/pre>\n<p><code>join()<\/code> is the most useful method here, because you control exactly how the items are separated. Read more: <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-array-join\/\">JavaScript array join()<\/a><\/p>\n<h2>3. String(), Template Literals and +<\/h2>\n<p>JavaScript converts an array to a string automatically when you use it as text. Internally it calls <code>toString()<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>console.log(String(fruits));\nconsole.log(`${fruits}`);\nconsole.log(\"Fruits: \" + fruits);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>apple,mango,banana\napple,mango,banana\nFruits: apple,mango,banana<\/code><\/pre>\n<p>So far our arrays were simple. But what happens with trickier values? Let&#8217;s check.<\/p>\n<h2>Nested Arrays, null and undefined<\/h2>\n<pre class=\"wp-block-code\"><code>console.log([1, [2, 3], [4, [5]]].toString());\nconsole.log([1, null, undefined, 4].join(\"-\"));\nconsole.log([].join(\", \") === \"\");<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>1,2,3,4,5\n1---4\ntrue<\/code><\/pre>\n<p>Nested arrays are flattened into one list, <code>null<\/code> and <code>undefined<\/code> become empty strings, and an empty array gives an empty string.<\/p>\n<p>Now here is a situation that confuses many beginners: an array of objects.<\/p>\n<h2>Arrays of Objects: Use map() First<\/h2>\n<pre class=\"wp-block-code\"><code>const users = [{ name: \"Riya\" }, { name: \"Aman\" }];\nconsole.log(users.toString());\nconsole.log(users.map(u =&gt; u.name).join(\", \"));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[object Object],[object Object]\nRiya, Aman<\/code><\/pre>\n<p>Objects turn into the unhelpful <code>[object Object]<\/code>. Pick the field you want with <code>map()<\/code>, then <code>join()<\/code>.<\/p>\n<p>All the methods above are good for showing data to people. But when you need to save data or send it to a server, there is a better tool.<\/p>\n<h2>JSON.stringify(): When You Need to Store or Send Data<\/h2>\n<pre class=\"wp-block-code\"><code>console.log(JSON.stringify(fruits));\nconsole.log(JSON.stringify(users));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[\"apple\",\"mango\",\"banana\"]\n[{\"name\":\"Riya\"},{\"name\":\"Aman\"}]<\/code><\/pre>\n<p>Use <code>JSON.stringify()<\/code> for <code>localStorage<\/code> or APIs, because you can turn it back into the same array with <code>JSON.parse()<\/code>. A comma-joined string cannot reliably be turned back if an item itself contains a comma.<\/p>\n<h2>Practical Examples<\/h2>\n<h3>Readable Sentence from a List<\/h3>\n<pre class=\"wp-block-code\"><code>const names = [\"Riya\", \"Aman\", \"Kabir\"];\nconst sentence = names.slice(0, -1).join(\", \") + \" and \" + names.at(-1);\nconsole.log(sentence);\n\nconst lf = new Intl.ListFormat(\"en\", { style: \"long\", type: \"conjunction\" });\nconsole.log(lf.format(names));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Riya, Aman and Kabir\nRiya, Aman, and Kabir<\/code><\/pre>\n<h3>Numbers in Indian Format<\/h3>\n<pre class=\"wp-block-code\"><code>console.log([1234567.891, 99].map(n =&gt; n.toLocaleString(\"en-IN\")).join(\" \/ \"));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>12,34,567.891 \/ 99<\/code><\/pre>\n<h3>Back from String to Array<\/h3>\n<pre class=\"wp-block-code\"><code>console.log(\"apple,mango,banana\".split(\",\"));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 'apple', 'mango', 'banana' ]<\/code><\/pre>\n<h2>Which Method Should You Use?<\/h2>\n<p>Friends, with so many options it is easy to get confused. Here is a quick summary:<\/p>\n<table>\n<thead>\n<tr>\n<th>Need<\/th>\n<th>Use<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Quick comma list<\/td>\n<td><code>toString()<\/code> or <code>String(arr)<\/code><\/td>\n<\/tr>\n<tr>\n<td>Custom separator (&#8220;, &#8220;, &#8221; | &#8220;, new lines)<\/td>\n<td><code>join(separator)<\/code><\/td>\n<\/tr>\n<tr>\n<td>Array of objects<\/td>\n<td><code>map()<\/code> then <code>join()<\/code><\/td>\n<\/tr>\n<tr>\n<td>Save or send data<\/td>\n<td><code>JSON.stringify()<\/code><\/td>\n<\/tr>\n<tr>\n<td>Natural sentence (&#8220;A, B and C&#8221;)<\/td>\n<td><code>Intl.ListFormat<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So friends, remember the key points: both <code>toString()<\/code> and <code>join()<\/code> return a string, but <code>toString()<\/code> always separates items with a comma, while <code>join()<\/code> lets you choose the separator. Neither of them changes the original array. And when you need to turn the string back into an array, use <code>split()<\/code> for a plain list, or <code>JSON.parse()<\/code> if you saved it with <code>JSON.stringify()<\/code>. Try each method in your browser console and you will remember the differences easily.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/arrays-in-javascript\/\">Arrays in JavaScript<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/array-methods-in-javascript\/\">Array methods<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-array-length\/\">Array length<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Converting an array to a string is something you do all the time in JavaScript: showing a list of names on a page, building a sentence, saving data or sending it to a server. There are several ways to do it, and each gives a slightly different result. In this lesson, let&#8217;s compare &#8230; <a title=\"JavaScript Array to String: toString(), join() and More\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/javascript-array-to-string\/\" aria-label=\"Read more about JavaScript Array to String: toString(), join() and More\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-1040","post","type-post","status-publish","format-standard","hentry","category-javascript-tutorial","tag-javascript","tag-javascript-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=1040"}],"version-history":[{"count":5,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1040\/revisions"}],"predecessor-version":[{"id":6144,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1040\/revisions\/6144"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}