[JavaScript] 배열 하나의 문자열로 합치기 (join())

arr.join([separator])

separator 안에는 합쳐진 배열의 요소를 구분할 수 있는 기호가 들어감

() -> 쉼표로 구분, ("") -> 공백없이 구분

에 주의!

 

var arrChar = ['hello', 'world', '!'];

var ex1 = arrChar.join(); 
var ex2 = arrChar.join(", ");
var ex3 = arrChar.join(""); 
var ex4 = arrChar.join("-"); 

console.log(ex1); //hello,world,!
console.log(ex2); //hello, world, !
console.log(ex3); //helloworld!
console.log(ex4); //hello-world-!

 

참고자료:

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/join