Arrays
An array is a data structure that contains any number of items (or elements
). In JavaScript, array elements can be of any data type.
Create an empty array:
Elements in the array should be separated by commas.
Accessing Elements
Each element in the array has a numbered index
. The first element has an index of 0
.
Access elements by putting the index number
in square brackets.
Getting the Number of Elements in an Array
We can use the .length
property to find out the number of elements in an array
Changing Elements
To change an element in an array, first access the element, and then assign a new value:
NOTE: const
only prevents reassignment, the value can still be mutated.
Iterate over an array
Let's use a for..of
loop to iterate over the array and print each element out.
Last updated
Was this helpful?