Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DoctorDerek/3d413e3476053cd0beca671b0ecb2a12 to your computer and use it in GitHub Desktop.
Save DoctorDerek/3d413e3476053cd0beca671b0ecb2a12 to your computer and use it in GitHub Desktop.
How To Turn an Array Into a String Without Commas in JavaScript https://medium.com/p/241598bb054b
const string = "🍌,B,A,N,A,N,A,S"
// .split() searches for a pattern
const array = string.split(",")
// Here, "," is the separator string
console.log(array)
// true
// .join() is the same as .join(",")
console.log(array.join())
// ["🍌","B","A","N","A","N","A","S"]
// Use "" for a string without commas
console.log(array.join(""))
// 🍌,B,A,N,A,N,A,S
// We can remake the original string
console.log(array.join()===string)
// 🍌BANANAS
// Here's a second example with emoji
const string2 = "😏,🀩,😎,πŸ˜‹,πŸ˜‰"
const array2 = string2.split(",")
console.log(array2)
// ["😏","🀩","😎","πŸ˜‹","πŸ˜‰"]
// .join() is the same as .join(",")
console.log(array2.join())
// 😏,🀩,😎,πŸ˜‹,πŸ˜‰
// Again, empty string "" === no commas
console.log(array2.join(""))
// πŸ˜πŸ€©πŸ˜ŽπŸ˜‹πŸ˜‰
// We can remake the original string
console.log(array2.join()===string2)
// true
// You can use any separator string:
console.log(array.join(string2))
// 🍌😏,🀩,😎,πŸ˜‹,πŸ˜‰B😏,🀩,😎,etc.
// Here's a third example, no emoji:
const string3 = "The quick brown dog"
const array3 = string3.split("")
console.log(array3)
// ["T","h","e"," ","q","u","i",etc.
// .join() is the same as .join(",")
console.log(array3.join())
// T,h,e,,q,u,i,c,k,,b,r,o,w,n,,d,o,g
// Again, empty string "" === no commas
console.log(array3.join(""))
// The quick brown dog
// We can remake the original string
console.log(array3.join("")===string3)
// true
// You can use any separator string:
console.log(array.join(string3))
// 🍌The quick brown dogBThe quick etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment