Access Array Elements in JavaScript
JavaScript·2 min read·Jan 1, 2025
Each element of an array is associated with a unique numerical key called an index, which represents its position within the array.
The 1st element of the array is located at index 0, the 2nd element at index 1, and so on.
To access the value stored at a specific index, you can use the following syntax:
array[index]Where:
arrayis an array instance.indexis either a user-defined number, a variable containing a number, or a computed number.
Note: When trying to access a non-existent array index, the returned value will be
undefined.
Example
Let's consider this script:
const rgb = [255, 192, 203];console.log('red = ', rgb[0]);console.log('green = ', rgb[1]);