Arrays
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Arrays hold lists
- An array is an ordered list of values in
[ ... ]. - Reach an item by its index, counting from
0:arr[0]is the first. .lengthtells you how many items there are.
한국어
배열은 목록을 담습니다
- 배열(array) 은
[ ... ]순서에 맞춰 정렬된 값의 목록입니다. - 항목에 인덱스로 접근하려면
0부터 세어去吧:arr[0]가 첫 번째입니다. .length은 항목의 총 개수를 알려줍니다.
let nums = [10, 20, 30];
console.log(nums[0]); // → 10
console.log(nums.length); // → 3
English
Add and remove items
.push(x)addsxto the end;.pop()removes the last item.- The array grows and shrinks as you go.
한국어
항목 추가 및 제거
.push(x)은x을 마지막에 추가하고,.pop()은 마지막 항목을 제거합니다.- 작업하는 동안 배열이 커지거나 작아집니다.
let nums = [10, 20, 30];
nums.push(40);
console.log(nums); // → [10,20,30,40]
nums.pop();
console.log(nums); // → [10,20,30]
Explore · 탐색하기
Index, push and pop
Items are numbered from 0. push adds to the end; pop removes from the end.
English
Loop over an array
- A
forloop with an index visits each item, from0tolength - 1.
한국어
배열 순환하기
- 인덱스를 사용하는
for루프는0부터length - 1까지 각 항목을 방문합니다.
let nums = [1, 2, 3];
for (let i = 0; i < nums.length; i++) {
console.log(nums[i]); // → 1, then 2, then 3
}
English
Common mistakes
- Index from
0;a.lengthis the size. pushadds to the end;popremoves from the end.
한국어
흔한 실수
- 인덱스는
0부터 시작하며,a.length은 크기(size)입니다. push은末尾에 추가하고,pop은末尾에서 제거합니다.
English
Now you try
- Use an index, or a loop, on each array.
- Press Check answer to test it.
한국어
이제 직접 해보기
- 각 배열에 인덱스나 루프를 사용하세요.
- 답안 확인을 눌러 테스트해 보십시오.
let colours = ["red", "green", "blue"]. Log the second item, colours[1] (it is green).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
let nums = [4, 8, 15, 16]. Log how many items it has with .length (it is 4).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
let stack = [1, 2]. Use .push to add 3, then log the array (it is [1,2,3]).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Use a for loop to add up every number in nums and log the total (it is 43).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.