[JS] array.join 메소드

SINGING BIRD·2023년 4월 11일

역할

배열의 요소들을 합쳐서 하나의 문자열로 만들어주는 메소드입니다.


예시

const salaryList = [5300, 4000, 3000]
const companyList = ['sam', 'lg', 'node']

// 1 - 매개변수가 없을 때 
console.log(salaryList.join())
console.log(companyList.join())

// "5300,4000,3000"
// "sam,lg,node"
// separator 가 없는 경우 기본 , 콤마로 합쳐줍니다.


// 2 - 매개변수가 있을 때
console.log(salaryList.join(''))
console.log(companyList.join(''))

// "530040003000"
// "samlgnode"
// 문자열을 그대로 붙이고 싶으면 빈문자열 '' 을 넣어줍니다.
// ** salaryList 는 숫자들이지만 문자열 '' 와 더해지면서 string 타입으로 + 계산되었습니다. 
profile
good things take time

0개의 댓글