Course Content
JavaScript Data Structures
JavaScript Data Structures
Mastering Arrays Sum Up
Arrays are data structures used to store and manipulate collections of values.
Creating Arrays
- Arrays in JavaScript are created using square brackets
[]
and are known as array literals; - Commas separate elements within the array.
Accessing Array Elements
- Array indices start at
0
, meaning the first element has an index of0
, the second element has an index of1
, and so on; - Specific elements within an array can be accessed by using square brackets with the element's index.
Modifying Array Elements
Array elements can be changed by accessing them through their index and assigning a new value.
Array Length
The length
property of an array represents the number of elements it contains. It automatically adjusts when elements are added or removed.
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, which is array.length - 1
.
For Loop
- The
for
loop is used for iterating over an array element by element; - It uses a loop counter variable (e.g.,
i
) to access each element by index; - The loop continues as long as the counter is less than the array's length;
- It's a fundamental tool for array iteration.
For...of Loop
- The
for...of
loop is a more modern and concise way to iterate over arrays; - It automatically handles the loop counter and provides direct access to each element's value;
- It simplifies array iteration, making the code cleaner and more readable.
Thanks for your feedback!