Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 18, 2022 11:06 am GMT

Array to String Without Commas in JavaScript

In this article, youll 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:

Thanks for reading. Lets connect!

I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more.


Original Link: https://dev.to/gaelgthomas/array-to-string-without-commas-in-javascript-4mg6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To