Last active
January 27, 2021 18:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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