배열 [ ]

(Array)

서로 연관된 여러개의 데이터를 하나의 변수에 저장하고 관리하기 위해서 사용하는 데이터 타입

변수가 하나의 데이터저장하기 위한 것이라면, 배열은 여러 개의 데이터하나의 변수에 저장하기 위한 것

대괄호로 시작해서 대괄호로 끝남 ex) ["west", "silver"]

1. 배열의 생성과 데이터 불러오기

var name = ['egoing', 'leezche', 'taeho']
alert(name[0]); //egoing
alert(name[1]); //leezche
alert(name[2]); //taeho

예시) 데이터 불러오기, 개수 세기, 추가

<h1>Array</h1>
<h2>Syntax(문법)</h2>
<script>
    var coworkers = ["west", "silver"];
</script>

<h2>get</h2>      //데이터 불러오기
<script>
    document.write(coworkers[0]);
    document.write(coworkers[1]);
</script>

<h2>count</h2>    //개수 세기
<script>
    document.write(coworkers.length);
</script>

<h2>add</h2>      //데이터 추가
<script>
    coworkers.push('seoeun');
    coworkers.push('redcho');
</script>

<h2>count</h2>    //개수 세기
<script>
    document.write(coworkers.length);
</script>
  • 배열 : 데이터 불러오기

  • .length : 개수 세기

  • .push : 데이터 추가

매개변수

의미

element

배열의 요소

index

배열 요소의 인덱스

array

forEach를 호출한 배열

Last updated