This flow shows how we start with an array, access a specific index, read or update the value there, and then finish.
Execution Sample
DSA Javascript
let arr = [10, 20, 30, 40];
let val = arr[2];
arr[1] = 25;
console.log(arr);
This code reads the value at index 2 and updates the value at index 1 in the array.
Execution Table
Step
Operation
Index Accessed
Value Read
Value Updated
Array State
1
Initialize array
-
-
-
[10, 20, 30, 40]
2
Access value at index 2
2
30
-
[10, 20, 30, 40]
3
Update value at index 1 to 25
1
-
25
[10, 25, 30, 40]
4
Print array
-
-
-
[10, 25, 30, 40]
💡 All operations completed; array updated at index 1.
Variable Tracker
Variable
Start
After Step 2
After Step 3
Final
arr
[10, 20, 30, 40]
[10, 20, 30, 40]
[10, 25, 30, 40]
[10, 25, 30, 40]
val
undefined
30
30
30
Key Moments - 3 Insights
Why does accessing arr[2] give 30 and not 20 or 40?
Because arrays start counting at index 0, so index 2 points to the third element, which is 30, as shown in step 2 of the execution table.
What happens if we update arr[1] to 25? Does it change the whole array?
Only the element at index 1 changes to 25; other elements stay the same. This is shown in step 3 where the array changes from [10, 20, 30, 40] to [10, 25, 30, 40].
If we try to access an index outside the array, what happens?
Accessing an index outside the array returns undefined and does not change the array. This is not shown here but is important to remember.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'val' after step 2?
A20
B30
C25
Dundefined
💡 Hint
Check the 'Value Read' column at step 2 in the execution table.
At which step does the array change from [10, 20, 30, 40] to [10, 25, 30, 40]?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Array State' column in the execution table for changes.
If we tried to read arr[4], what would be the result?
A40
BError
Cundefined
D30
💡 Hint
Remember array indices start at 0 and the array length is 4; index 4 is outside.
Concept Snapshot
Array Access and Update:
- Access with arr[index] reads value at that position.
- Update with arr[index] = newValue changes that element.
- Indexing starts at 0.
- Accessing outside returns undefined.
- Only targeted element changes, rest stay same.
Full Transcript
This lesson shows how to access and update elements in an array by index. We start with an array of numbers. We read the value at index 2, which is the third element, 30. Then we update the value at index 1 from 20 to 25. The array changes only at that position. Accessing an index outside the array returns undefined. The execution table tracks each step and the array's state. The variable tracker shows how 'arr' and 'val' change. Key moments clarify common confusions about indexing and updates. The quiz tests understanding of these steps.
Practice
(1/5)
1. What does arr[2] return if arr = [10, 20, 30, 40]?
easy
A. 40
B. 20
C. 10
D. 30
Solution
Step 1: Understand array indexing
Arrays start at index 0, so index 2 is the third element.
Step 2: Identify the element at index 2
The element at index 2 in [10, 20, 30, 40] is 30.
Final Answer:
30 -> Option D
Quick Check:
Index 2 element = 30 [OK]
Hint: Remember arrays start at zero index [OK]
Common Mistakes:
Thinking index starts at 1
Confusing element value with index
Using negative index without explanation
2. Which of the following is the correct way to update the 3rd element of array nums to 50?
easy
A. nums[3] = 50;
B. nums(3) = 50;
C. nums[2] = 50;
D. nums{2} = 50;
Solution
Step 1: Recall array indexing syntax
In JavaScript, arrays use square brackets with zero-based index.
Step 2: Identify correct index for 3rd element
The 3rd element is at index 2, so nums[2] = 50; is correct.
Final Answer:
nums[2] = 50; -> Option C
Quick Check:
Use square brackets and zero-based index [OK]
Hint: Use square brackets and zero-based index [OK]