Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Working with Array Elements | Mastering Arrays
JavaScript Data Structures
course content

Conteúdo do Curso

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Objects Fundamentals
3. Advanced Object Manipulation
4. Mastering Arrays
5. Advanced Array Operations

bookWorking with Array Elements

Let's explore how to

  • Modify array elements;
  • Find the length of an array;
  • Access the last element and delve deeper into the concept of indexing.

Modifying Array Elements

Array elements can be changed by accessing them through their index and assigning a new value.

12345
const weekdays = ["Monday", "Tuesday", "Wednesday"]; weekdays[0] = "Lunes"; weekdays[1] = "Martes"; console.log(weekdays); // Output: Lunes, Martes, Wednesday
copy

Here, we modify the first two elements of the weekdays array to their Spanish translations.

Array Length

The length property of an array represents the number of elements it contains. This property is dynamic and automatically adjusts when elements are added or removed.

123
const mountains = ["Everest", "K2", "Kangchenjunga", "Lhotse"]; console.log(mountains.length); // Output: 4
copy

In this example, the mountains array contains four elements, and mountains.length returns 4.

Finding the Last Element

To retrieve the value of the last element in an array, we can calculate its index using the array's length. The index of the last element is always array.length - 1.

1234
const vehicles = ["car", "bike", "bus", "train"]; const lastIndex = vehicles.length - 1; console.log(vehicles[lastIndex]); // Output: train
copy

In this case, lastIndex is computed as 3, which is the index of the last element, and we access the last element using vehicles[lastIndex].

1. How can we modify an array element?
2. What is the purpose of the `length` property of an array?
3. In the example provided below, what is the new value of the first element in the `movies` array after the modification?
4. How can we access the last element in the `cartoons` array?
5. What is the value of the `item` in the example provided below?
How can we modify an array element?

How can we modify an array element?

Selecione a resposta correta

What is the purpose of the `length` property of an array?

What is the purpose of the length property of an array?

Selecione a resposta correta

In the example provided below, what is the new value of the first element in the `movies` array after the modification?

In the example provided below, what is the new value of the first element in the movies array after the modification?

Selecione a resposta correta

How can we access the last element in the `cartoons` array?

How can we access the last element in the cartoons array?

Selecione a resposta correta

What is the value of the `item` in the example provided below?

What is the value of the item in the example provided below?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt