DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Array to String Without Commas in JavaScript
GaΓ«l Thomas
GaΓ«l Thomas

Posted on • Originally published at herewecode.io

Array to String Without Commas in JavaScript

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

If you want to go further, you can learn how to:


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)

Rust language vs others

Stop by this week's meme thread!