<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>index6.html 파일입니다.</h1>
<script>
// 배열을 사용하는 방법 2가지를 살펴보자.
// new 키워드와 생성자를 사용하여 배열을 생성할 수 있다.
const numbers = new Array(1, 2, 3, 4, 5);
// 상태값도 변경 못하게 막으려면 최상위 object에 freeze 사용하면 된다.
Object.freeze(numbers);
console.log(numbers[0]);
numbers[0] = 100000;
console.log(numbers[0]);
// 배열 리터럴 : 대괄호 [ ] 를 사용하여 배열을 생성할 수 있다.
const fruits = ['apple','banana','cherry'];
// 배열 요소에 접근하기
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
// 배열 길이 확인하기!
console.log(fruits.length);
// 배열에 요소 추가하기 - push() 메서드를 사용하여 배열 끝에 요소를 추가할 수 있다.
fruits.push('peach');
console.log(fruits);
// 배열 요소 제거하기 - pop() 메서드를 사용하여 배열의 끝에 있는 요소를 삭제할 수 있다.
fruits.pop();
console.log(fruits);
// 배열에 요소를 순회
for(i = 0; i < numbers.length; i++){
console.log(numbers[i]);
}
console.log('-------------------------------------');
numbers.forEach(function(num){
console.log(num);
});
</script>
</body>
</html>