티스토리 뷰

Javascript

배열 객체(1)

GYChoi 2022. 2. 3. 17:54

배열 속성

length:배열의 길이 구하기(반환:숫자)

{
    const arrNum = [100, 200, 300, 400, 500];
    const arrNumLength = arrNum.length;

    const arrText = ['a', 'b', 'c', 'd', 'e'];
    const arrTextLength = arrText.length;
    
    const arr = [1,2, ['a', 'b']];
    const arrLength = arr.length;
}
기본값 속성 리턴값
[100, 200, 300, 400, 500] .length 5
['a', 'b', 'c', 'd', 'e'] .length 5
[1,2, ['a', 'b']] .length 3

배열 메서드

join():배열 요소 문자열 결합(반환:문자열)

{
    const arrNum = [100, 200, 300, 400, 500];
    
    const text1 = arrNum.join("*");
    const text2 = arrNum.join("-");
    const text3 = arrNum.join("");
    const text4 = arrNum.join(" ");
    const text5 = arrNum.join("+");
}
기본값 메서드 결괏값
[100, 200, 300, 400, 500] .join('*') 100*200*300*400*500
[100, 200, 300, 400, 500] .join('-') 100-200-300-400-500
[100, 200, 300, 400, 500] .join('') 100200300400500
[100, 200, 300, 400, 500] .join(' ') 100 200 300 400 500
[100, 200, 300, 400, 500] .join('+') 100+200+300+400+500

 

push():배열 마지막 요소에 추가(반환:숫자)
pop():배열 마지막 요소 삭제(삭제된 요소)

{
    const arrNum = [100, 200, 300, 400, 500];
    const arrPush = arrNum.push(600)
}
{
    const arrNum = [100, 200, 300, 400, 500];
    const arrPop = arrNum.pop()
}
기본값 메서드 리턴값 결괏값
[100, 200, 300, 400, 500] .push(600) 6 100,200,300,400,500,600
[100, 200, 300, 400, 500] .pop() 500 100,200,300,400

'Javascript' 카테고리의 다른 글

배열 객체(3)  (1) 2022.02.06
배열 객체(2)  (0) 2022.02.04
for문 응용하기  (0) 2022.02.02
콜백함수  (0) 2022.01.27
Javascript 기초(2)  (0) 2022.01.25
댓글
© 2018 webstoryboy