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.
Русский
Массивы содержат списки
- Массив — это упорядоченный список значений в
[ ... ]. - Обращайтесь к элементу по его индексу, нумерация начинается с
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— размер. 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. · Нажмите Запустить, чтобы увидеть результат здесь.