> For the complete documentation index, see [llms.txt](https://ayou09110.gitbook.io/javascript-jquery/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayou09110.gitbook.io/javascript-jquery/undefined-5.md).

# 배열

## 배열

배열은 여러개의 데이터를 순차적으로 저장하는 저장소입니다.

```javascript
var 변수명 = [/*값1, 값2,값3*/]
```

### 배열 선언

```javascript
let arr1 = new Array();
arr1[0] = 100;
arr1[1] = 200;

document.write(arr1[0], "<br />");
document.write(arr1[1]), "<br />");

//100
//200
```

### 배열을 선언과 동시에 초기화

```javascript
let arr2 =  new Array(100, 200, 300);

document.write(arr2[0], "<br>");
document.write(arr2[1], "<br>");
document.write(arr2[2], "<br>");
```

### 배열을 선언하지 않고 초기화

```javascript
let arr3 = [100, 200, 300]

document.write(arr3[0],"<br>");
document.write(arr3[1],"<br>");
document.write(arr3[3],"<br>");
```

### 배열의 크기

```javascript
let arr4 = [100,200,300,400,500];

document.write(arr4.length);
```

### 배열 가져오기

```javascript
let arr1 = [100, 200, 300, 400, 500, 700, 800, 900];

/*document.write(arrl[0],"<br>")
document.write(arrl[1],"<br>")
document.write(arrl[2],"<br>")
document.write(arrl[3],"<br>")
document.write(arrl[4],"<br>")
document.write(arrl[5],"<br>")
document.write(arrl[6],"<br>")
document.write(arrl[7],"<br>")
document.write(arrl[8],"<br>")
document.write(arrl[9],"<br>")
document.write(arrl[10],"<br>")*/

for(let i=0; i<=arr1.length; i++){
    document.write(arr1[i], "<br>");
}
```

#### 배열의 합 구하기

```javascript
let arr2 = [100, 200, 300, 400, 500];
let sum = 0;

for(let i = 0; i<arr2.length, i++){
    //arr2[0] = 100
    //arr2[1] = 200
    //arr2[2] = 300
    //arr2[3] = 400
    //arr2[4] = 500
    //arr2[5] = 600
    //arr2[6] = 700 
    //arr2[7] = 800
    //arr2[8] = 900
    sum = sum + arr2[i]
}
document.write(sum);
```

#### forEach 사용하기

```javascript
const arr3 = [100,200,300,400,500,600,700,800,900];

arr3.forEach((element, index, array) => {
    document.write(element, "<br>");
    document.write(index, "<br>");
    document.write(array, "<br>");
})
```

#### for in 문

```javascript
const arr3 = [100,200,300,400,500,600,700,800,900];
for(let i in arr3){
    document.write(arr3[i]);
}
```

#### for of 문

```javascript
const arr3 = [100,200,300,400,500,600,700,800,900];
for(let i of arr3){
    document.write(i);
}
```

#### 배열 비구조화 할당

```javascript
let [x, y] = [100,200];

document.write(x);
document.write(y);
document.write("<br><br>");

[x, y] = [y, x]

document.write(x);
document.write(y);
```
