Concept Flow - Accessing array elements
Start
Have an array
Choose index number
Access element at index
Use or display element
End
This flow shows how to get a value from an array by picking its position number (index).
const fruits = ['apple', 'banana', 'cherry']; const first = fruits[0]; console.log(first);
| Step | Action | Expression | Result | Explanation |
|---|---|---|---|---|
| 1 | Create array | const fruits = ['apple', 'banana', 'cherry'] | fruits = ['apple', 'banana', 'cherry'] | Array with 3 elements created |
| 2 | Access element | fruits[0] | 'apple' | Get element at index 0 (first element) |
| 3 | Assign to variable | const first = fruits[0] | first = 'apple' | Variable first stores 'apple' |
| 4 | Print value | console.log(first) | apple | Output the value of first |
| 5 | End | - | - | Program ends |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| fruits | undefined | ['apple', 'banana', 'cherry'] | ['apple', 'banana', 'cherry'] | ['apple', 'banana', 'cherry'] | ['apple', 'banana', 'cherry'] |
| first | undefined | undefined | undefined | 'apple' | 'apple' |
Access array elements using bracket notation: array[index] Index starts at 0 for the first element. Accessing an invalid index returns undefined. Store accessed element in a variable to use it. Use console.log() to print the element.