Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Modifying Arrays and Accessing Elements | Mastering JavaScript Arrays
JavaScript Data Structures

book
Modifying Arrays and Accessing 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.

const weekdays = ["Monday", "Tuesday", "Wednesday"];
weekdays[0] = "Lunes";
weekdays[1] = "Martes";

console.log(weekdays); // Output: Lunes, Martes, Wednesday
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.

const mountains = ["Everest", "K2", "Kangchenjunga", "Lhotse"];

console.log(mountains.length); // Output: 4
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.

const vehicles = ["car", "bike", "bus", "train"];
const lastIndex = vehicles.length - 1;

console.log(vehicles[lastIndex]); // Output: train
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?

question mark

How can we modify an array element?

Select the correct answer

question mark

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

Select the correct answer

question mark

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

const movies = ["The Shawshank Redemption", "The Godfather", "The Dark Knight"];

movies[1] = "Pulp Fiction";

movies[0] = "Inception";

Select the correct answer

question mark

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

const cartoons = [
"Tom and Jerry",
"The Simpsons",
"Rick and Morty",
"South Park",
"The Flintstones",
];

console.log(___);

Select the correct answer

question mark

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

const farms = ["Orchard", "Vineyard", "Horse Ranch", "Organic Farm"];

const item = farms[farms.length - 1];

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
We use cookies to make your experience better!
some-alt