In this article, you’ll discover how to convert an array to a string without commas. By no commas, I mean no separator between your array elements (words) or a separator different than a comma.
How to Convert Array to String Without Commas
In JavaScript, all arrays have a built-in method called join()
. You can use it to convert an array to a string without commas. You can call the join method with an empty string as a parameter (join("")
). The method will return a string with all the array elements concatenated.
Concrete Example: Join Array to String
Without Commas
As mentioned in the above paragraph, you can use the join method to create a string without commas from your array.
This method works on an Array and takes up to one parameter:
-
no parameter: your array will be joined with commas (
["hello", "world"].join()
) -
with parameter: your array will be joined with the string provided as a parameter (
["hello", "world"].join("-")
)
Let me give you an example without commas:
const helloMagicWorldArray = ["Hello", "Magic", "World"]
const helloMagicWorldString = helloMagicWorldArray.join("")
console.log(helloMagicWorldString)
// Output: "HelloMagicWorld"
With Separator
I suppose you start the get the idea! If you want to join your array with something different than commas, you can pass the separator of your choice.
const helloMagicWorldArray = ["Hello", "Magic", "World"]
console.log(helloMagicWorldArray.join("/"))
// Output: "Hello/Magic/World"
console.log(helloMagicWorldArray.join(" - "))
// Output: "Hello - Magic - World"
If you want to go further, you can learn how to:
- filter an array in JavaScript
- replace item in array with JavaScript
- remove null values from an array in JavaScript
Thanks for reading. Let’s connect!
➡️ I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more. 🚀🎒
Top comments (0)