◼ concat
MDN 문서 - concat
- 파라미터로 들어온 다른 배열이나 값을 넣어 새로운 배열을 반환
- 기존 배열은 유지
배열명.concat(배열명 또는 값)
형태로 작성한다.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const concated1 = arr1.concat(arr2);
const concated2 = arr2.concat(arr1);
const concated3 = arr2.concat(4);
console.log(concated1);
console.log(concated2);
console.log(concated3);
◼ join
MDN 문서 - join
- 배열의 각 요소들을 연결해 하나의 문자열로 반환
배열명.join(seperator)
형태로 작성한다. seperator는 옵션이기에 넣어도 되고 안 넣어도 된다.
- seperator에 아무 인자도 넣지 않으면 각 배열요소 사이에는 쉼표(,)가 들어간다.
- seperator에
''
를 넣으면 배열 요소 사이에는 아무 문자열도 들어가지 않게 된다.
const array = [4, 5, 6];
console.log(array.join());
console.log(array.join(""));
console.log(array.join(" "));
console.log(array.join("+"));
console.log(array.join("+-"));
◼ 요약
- 배열에 배열이나 값을 이어붙인 새로운 배열을 만들고 싶을 때 concat()
- 배열의 각 요소들을 하나의 문자열로 만들고 싶을 때 join ()