JS : Array.join()

daymoon_·2022년 3월 9일
0

JAVASCRIPT

목록 보기
8/23
post-thumbnail

Array.join()

🔗 참고자료
MDN Array.prototype.join()
CODINGFACTORY Array.join()
WebClub 배열 메서드 join()

join()은 배열의 원소들을 연결하여 하나의 문자열로 만들어 준다.


반환 값

  1. 배열의 모든 요소들을 하나의 문자열로 반환 ▶ String
  2. arr.length0이면 빈 문자열 반환 ▶ String
  3. 요소가 undefined 또는 null이면 빈 문자열로 반환 ▶ String

📌 예시

const lst = ['red', 'blue', 'yellow', 'green'];

// () : 원소들을 콤마(,)로 구분
// red,blue,yellow,green
console.log(lst.join());


// ('') : 원소 구분 없음
// redblueyellowgreen
console.log(lst.join(''));


// ('-') : 원소들을 -로 구분
// red-blue-yellow-green
console.log(lst.join('-'));

// ('--') : 원소들을 --로 구분
// red--blue--yellow--green
console.log(lst.join('--'));


// ('*') : 원소들을 *로 구분
// red*blue*yellow*green
console.log(lst.join('*'));
profile
미지의 공간🌙

0개의 댓글