Accessing and modifying Array elements

Accessing and modifying array elements is an important part of working with arrays in JavaScript. You can access individual elements of an array by using their index number.

In JavaScript, array indexes start at 0, so the first element in an array has an index of 0, the second element has an index of 1, and so on.

Here's an example:

Loading Code . . .

In the example above, we create an array called myArray with four elements. We then assign the first element of the array to the variable firstElement and the second element of the array to the variable secondElement.

Modifying Array elements

You can also modify the elements of an array by assigning a new value to the element's index number, like when you assign a new value to a variable.

Loading Code . . .

In the example above, we assign the string 'Alice Cooper' to the first element of the array myArray.

Array length

The length property of an array returns the number of elements in the array.

Loading Code . . .

The length property of the array myArray is 4, because the array has four elements. We'll learn more about properties in the next section.

Just try to remember that the length property is accessed by using the dot operator (.) after the array name, and that the length property returns the number of elements in the array.

Accessing the last element of an array

As you may have noticed, the last element of an array has an index number that is one less than the array's length property. This is because array indexes start at 0, but we count from 1.

Loading Code . . .

In the example above, we create an array called myArray with four elements. We then assign the number 4 to the variable totalElements, which is the value of the length property of the array myArray. We then assign the number 3 to the variable lastElementIndex, which is the value of totalElements minus 1. We then assign the last element of the array myArray to the variable lastElement.

For sure, this was just to show you how to access the last element of an array in an easy way to understand. You can also access the last element of an array by using the index number minus one, like this:

Loading Code . . .

This is basically the same as the example above, but we avoid creating variables that we don't need.

Array and const

You may have noticed that we used the const keyword to declare the array myArray in the examples above. But, if you try to assign a new value to the array myArray, you won't get an error.

Why is that? Isn't the const keyword supposed to prevent us from assigning a new value to a variable?

The answer is that the const keyword prevents us from assigning a new value to the variable myArray. But, it doesn't prevent us from modifying the elements of the array myArray.

Loading Code . . .

In the example above, we assign the string 'Alice Cooper' to the first element of the array myArray. This is allowed, because we're not assigning a new value to the variable myArray. We're just modifying the elements of the array myArray.

Then, we try to assign a new value to the variable myArray. This is not allowed, because we're trying to assign a new value to the variable myArray, which is not allowed when using the const keyword.

Code Time!

Now that you know how to access and modify array elements, let's practice a little bit.

In the code editor, you'll find a function called getFirstElement and getLastElement. Your job is to complete the function so that it returns the first and last element of the array that you are given as a parameter.

The items in the array will be random numbers to avoid cheating.

Loading...
Loading...
Loading...
Loading...
Loading...

© 2024 - ®Mewters