0
0
Javascriptprogramming~10 mins

Common array operations in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common array operations
Start with an array
Access elements by index
Add elements (push/pop, unshift/shift)
Modify elements
Search elements (indexOf, includes)
Iterate over elements (forEach, map)
End of operations
This flow shows how we start with an array and perform common operations like accessing, adding, modifying, searching, and iterating elements.
Execution Sample
Javascript
const arr = [10, 20, 30];
arr.push(40);
const first = arr[0];
arr[1] = 25;
const has30 = arr.includes(30);
arr.forEach(x => console.log(x));
This code creates an array, adds an element, accesses and modifies elements, checks for a value, and prints all elements.
Execution Table
StepOperationArray StateVariables ChangedOutput/Notes
1Initialize arr[10, 20, 30]arrArray created with 3 elements
2arr.push(40)[10, 20, 30, 40]arr40 added at the end
3Access arr[0][10, 20, 30, 40]first = 10first stores 10
4Modify arr[1] = 25[10, 25, 30, 40]arrSecond element changed from 20 to 25
5Check arr.includes(30)[10, 25, 30, 40]has30 = true30 is found in array
6arr.forEach(x => console.log(x))[10, 25, 30, 40]-Prints 10, 25, 30, 40 each on new line
7End[10, 25, 30, 40]arr, first, has30All operations done
💡 All common array operations executed step-by-step
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
arr[10, 20, 30][10, 20, 30, 40][10, 20, 30, 40][10, 25, 30, 40][10, 25, 30, 40][10, 25, 30, 40][10, 25, 30, 40]
firstundefinedundefined1010101010
has30undefinedundefinedundefinedundefinedtruetruetrue
Key Moments - 3 Insights
Why does arr.push(40) add the element at the end and not the beginning?
arr.push() always adds elements to the end of the array, unlike arr.unshift() which adds to the beginning. This is shown in execution_table step 2 where 40 is appended.
Why does modifying arr[1] change the second element and not the first?
Array indexes start at 0, so arr[1] refers to the second element. Step 4 in execution_table shows arr[1] changed from 20 to 25.
What does arr.includes(30) return if 30 is not in the array?
arr.includes(value) returns false if the value is not found. In step 5, it returns true because 30 is present.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of variable 'first'?
A10
B20
C30
Dundefined
💡 Hint
Check the 'Variables Changed' column at step 3 in execution_table
At which step does the array change from [10, 20, 30] to [10, 25, 30, 40]?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Array State' column in execution_table for each step
If we replaced arr.push(40) with arr.unshift(40), what would be the array state after step 2?
A[10, 20, 30, 40]
B[40, 10, 20, 30]
C[10, 20, 30]
D[40]
💡 Hint
unshift adds element to the start of the array, check step 2 array state for push
Concept Snapshot
Common array operations in JavaScript:
- Access elements by index: arr[0]
- Add elements: push() adds at end, unshift() at start
- Remove elements: pop() removes last, shift() removes first
- Modify elements by index
- Search with includes() or indexOf()
- Iterate with forEach() or map()
Full Transcript
This lesson shows common array operations in JavaScript. We start with an array of numbers. We add a new number at the end using push. We access the first element by index 0 and store it in a variable. We modify the second element by assigning a new value. We check if a number exists in the array using includes. Finally, we print all elements using forEach. The execution table tracks each step, showing how the array and variables change. Key moments clarify why push adds at the end, how indexing works, and what includes returns. The quiz tests understanding of variable values and array states during execution.