{"id":575,"date":"2022-07-21T13:55:18","date_gmt":"2022-07-21T13:55:18","guid":{"rendered":"https:\/\/typedarray.org\/?p=575"},"modified":"2022-07-21T13:55:19","modified_gmt":"2022-07-21T13:55:19","slug":"convert-array-to-string-without-commas-javascript","status":"publish","type":"post","link":"https:\/\/typedarray.org\/convert-array-to-string-without-commas-javascript\/","title":{"rendered":"Convert Array To String Without Commas In JavaScript"},"content":{"rendered":"\n

To convert array to string without commas in JavaScript, call the Array.join() method passing in an empty string as the parameter, like this – Array.join(”). This will concatenate all of the array elements into a single string with no commas between them.<\/strong><\/p>\n\n\n\n

Convert Array To String Without Commas In JavaScript<\/h2>\n\n\n
\n
\"convert<\/figure><\/div>\n\n\n

Array.prototype.join()<\/h3>\n\n\n\n

The Array.prototype.join()<\/a> method joins all elements of an array into a string and returns this string.<\/p>\n\n\n\n

Syntax<\/strong><\/p>\n\n\n\n

arr.join(separator)<\/p>\n\n\n\n

Parameters<\/strong><\/p>\n\n\n\n

separator<\/p>\n\n\n\n

Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma.<\/p>\n\n\n\n

If separator is an empty string, all elements are joined without any characters in between them.<\/p>\n\n\n\n

Return value<\/strong><\/p>\n\n\n\n

A string with all array elements joined.<\/p>\n\n\n\n

Convert An Array To A String Without Commas In JavaScript<\/h3>\n\n\n\n

If you have an array of items and you want to convert it to a string without adding any commas between the items, you can use the Array.prototype.join() method. This method accepts an optional separator parameter that you can use to specify a different character (or characters) to use as the separator. If you omit the separator parameter, a comma will be used by default.<\/p>\n\n\n\n

Here’s an example:<\/p>\n\n\n\n

var arr = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019];\n\nvar str = arr.join(");\n\nconsole.log(str); \/\/ \u2018abc\u2019<\/code><\/pre><\/div>\n\n\n\n

You can use any character (or characters) you want as the separator. For example, if you wanted to use a dash (-) as the separator, you could do this:<\/p>\n\n\n\n

var arr = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019];\n\nvar str = arr.join(\u2018-\u2018);\n\nconsole.log(str); \/\/ \u2018a-b-c\u2019<\/code><\/pre><\/div>\n\n\n\n

If you want to use space as a separator, you can do that too:<\/p>\n\n\n\n

var arr = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019];\n\nvar str = arr.join(\u2018 \u2018);\n\nconsole.log(str); \/\/ \u2018a b c\u2019<\/code><\/pre><\/div>\n\n\n\n

If you call the Array.join() method on an empty array, you’ll get an empty string:<\/p>\n\n\n\n

var arr = [];\n\nvar str = arr.join(");\n\nconsole.log(str); \/\/""<\/code><\/pre><\/div>\n\n\n\n

If you call the Array.join() method on an array with undefined or null as one of the elements, that element will be converted to the empty string:<\/p>\n\n\n\n

var arr = [\u2018a\u2019, \u2018b\u2019, undefined, \u2018c\u2019, null];\n\nvar str = arr.join(");\n\nconsole.log(str); \/\/\u2019abc\u2019<\/code><\/pre><\/div>\n\n\n\n

Keep in mind that the Array.join() method does not change the original array. It simply returns a new string.<\/p>\n\n\n\n

If you need to convert an array of values to a comma-separated string, you can even use the String() function passing the array as a parameter.<\/p>\n\n\n\n

Here’s an example:<\/p>\n\n\n\n

var arr = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019];\n\nvar str = String(arr);\n\nconsole.log(str); \/\/ \u2018a,b,c\u2019<\/code><\/pre><\/div>\n\n\n\n

Keep in mind that the String() function will add commas between the array elements.<\/p>\n\n\n\n

Conclusion<\/h2>\n\n\n\n

The Array.prototype.join() method is the easiest way to convert an array to string without commas in JavaScript. If you want to use a different character (or characters) as a separator, pass that character (or characters) as the parameter to the Array.prototype.join() method.<\/p>\n","protected":false},"excerpt":{"rendered":"

To convert array to string without commas in JavaScript, call the Array.join() method passing in an empty string as the parameter, like this – Array.join(”). This will concatenate all of the array elements into a single string with no commas between them. Convert Array To String Without Commas In JavaScript Array.prototype.join() The Array.prototype.join() method joins […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[12,8],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/575"}],"collection":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/comments?post=575"}],"version-history":[{"count":3,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/575\/revisions"}],"predecessor-version":[{"id":1177,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/575\/revisions\/1177"}],"wp:attachment":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/media?parent=575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/categories?post=575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/tags?post=575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}