Найти в Дзене

Arrays in JavaScript

One way we can create an array is to use an array literal. An array literal creates an array by wrapping items in square brackets []. Remember from the previous exercise, arrays can store any data type. We can also save an array to a variable. Each element in an array has a numbered position known as its index. // Обратная индексация не работает, в отличии от Python Update Elements Once you have access to an element in an array, you can update its value. // Изменение через переменную позволяет менять значения элементов списка Arrays with let and const Variables declared with let can be reassigned.
Variables declared with the const keyword cannot be reassigned. However, elements in an array declared with const remain mutable. Meaning that we can change the contents of a const array, but cannot reassign a new array or a different value. // Можно менять значения элементов списка, обозначенного через const, но невозможно поменять такой список полностью, заменив на новый The .length prop
JavaScript Tutorial: Learn JavaScript For Free | Codecademy

One way we can create an array is to use an array literal. An array literal creates an array by wrapping items in square brackets []. Remember from the previous exercise, arrays can store any data type.

We can also save an array to a variable.

-2

Each element in an array has a numbered position known as its index.

In the code snippet above: 
cities is an array that has three elements.
We’re using bracket notation, [] with the index after the name of the array to access the element.
cities[0] will access the element at index 0 in the array cities. You can think of cities[0] as accessing the space in memory that holds the string 'New York'.
In the code snippet above: cities is an array that has three elements. We’re using bracket notation, [] with the index after the name of the array to access the element. cities[0] will access the element at index 0 in the array cities. You can think of cities[0] as accessing the space in memory that holds the string 'New York'.

// Обратная индексация не работает, в отличии от Python

Update Elements

Once you have access to an element in an array, you can update its value.

-4

// Изменение через переменную позволяет менять значения элементов списка

Arrays with let and const

Variables declared with let can be reassigned.
Variables declared with the
const keyword cannot be reassigned. However, elements in an array declared with const remain mutable. Meaning that we can change the contents of a const array, but cannot reassign a new array or a different value.

// Можно менять значения элементов списка, обозначенного через const, но невозможно поменять такой список полностью, заменив на новый

The .length property

One of an array’s built-in properties is length and it returns the number of items in the array.

-5

The .push() Method

One method, .push() allows us to add items to the end of an array.

-6

The .pop() Method

Another array method, .pop(), removes the last item of an array.

-7

.shift()
The .shift() method removes the first item from the array.

.unshift()
The .unshift() method adds value between () to the beginning of list.

.slice()
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

.indexOf()
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Arrays and Functions
So when you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well.

Nested Arrays
When an array contains another array it is known as a nested array.

-8

To access the nested arrays we can use bracket notation with the index value.

-9

If we wanted to access the elements within the nested array we can chain, or add on, more bracket notation with index values.

-10