Introduction to arrays

Arrays are designed to work with datasets. Square brackets [] are used to create an array . Inside the square brackets, the elements of the array are defined. (Later in a separate chapter on arrays , we will look at additional ways to create arrays and work with arrays in general in more detail)

The simplest array definition:

const myArray = [];

In this case, we are creating an array called myArray. It is empty because no element is defined inside the square brackets. But you can also add initial data to it:

const people = ["Tom", "Alice", "Sam"];
console log(people); // ["Tom", "Alice", "Sam"];

In this case, the array myArray will have three elements. Graphically, it can be represented as follows:

Index Element
0 Tom
1 Alice
2 Sam

Indexes are used to access individual array elements. The counting starts from zero, that is, the first element will have an index of 0, and the last one will have an index of 2:

const people = ["Tom", "Alice", "Sam"];
console.log(people[0]); // Tom
const person3 = people[2]; // Sam
console log(person3); // Sam

If we try to access an element at an index greater than the size of the array, then we get undefined:

const people = ["Tom", "Alice", "Sam"];
console.log(people[7]); // undefined

Also, by index, values ​​are set for array elements:

const people = ["Tom", "Alice", "Sam"];
console.log(people[0]); // Tom
people[0] = "Bob";
console.log(people[0]); // Bob

Moreover, unlike other languages, like C# or Java, you can install an element that is not initially installed:

const people = ["Tom", "Alice", "Sam"];
console.log(people[7]); // undefined - array has only three elements
people[7] = "Bob";
console.log(people[7]); // Bob

It is also worth noting that, unlike in a number of programming languages, JavaScript arrays are not strongly typed, one array can store data of different types:

const objects = ["Tom", 12, true, 3.14, false];

Multidimensional arrays

Arrays can be one-dimensional or multidimensional. Each element in a multidimensional array can be a separate array. Above we considered a one-dimensional array, now we will create a multidimensional array:

const numbers1 = [0, 1, 2, 3, 4, 5 ]; // one-dimensional array
const numbers2 = [[0, 1, 2], [3, 4, 5] ]; // two-dimensional array

Visually, both arrays can be represented as follows:

One-dimensional array numbers1

0 1 2 3 4 5

2D array numbers2

0 1 2
3 4 5

Since the numbers2 array is two-dimensional, it is a simple table. Each element can represent a separate array.

Consider another two-dimensional array:

const people = [
        ["Tom", 25, false],
        ["Bill", 38, true],
        ["Alice", 21, false]

console.log(people[0]); // ["Tom", 25, false]
console.log(people[1]); // ["Bill", 38, true]

The people array can be represented as the following table:

Tom 25 false
Bill 38 true
Alice 21 false

To get an individual element of an array, an index is also used:

const tomInfo = people[0];

Only now the variable tomInfowill represent an array. To get an element inside a nested array, we need to use its second dimension:

console.log("Name: " + people[0][0]); // Tom
console.log("AGE: " + people[0][1]); // 25

That is, if we can visually represent a two-dimensional array as a table, then the element people[0][1]will refer to the table cell located at the intersection of the first row and the second column (the first dimension is 0 – row, the second dimension – 1 – column).

We can also do an assignment:

const people = [
        ["Tom", 25, false],
        ["Bill", 38, true],
        ["Alice", 21, false]
people[0][1] = 56; // assign a separate value
console.log(people[0][1]); // 56
people[1] = ["Bob", 29, false]; // assign an array
console.log(people[1][0]); // Bob

When creating multi-dimensional arrays, we are not limited to only two-dimensional ones, but we can also use arrays of large dimensions:

const numbers = [];
numbers[0] = []; // now numbers is a two-dimensional array
numbers[0][0]=[]; // now numbers is a 3D array
numbers[0][0][0] = 5; // the first element of the 3D array is 5