🎯 Learning Objectives
By the end of this lecture, students will be able to:
- Use modern array looping methods (
forEach, map, filter, find)
- Understand which methods return a new array and which change the original
- Detect arrays using
Array.isArray()
- Combine and copy arrays using the spread operator (
...)
- Destructure arrays
- Understand how sorting works in JavaScript
- Deal with custom/non-sequential array indexes (sparse arrays)
- Write more readable and modern JavaScript code
🟨 1. forEach() — Loop Over an Array
✔ What it does
Executes a function for each item in the array.
✔ Does it return a new array?
❌ No, it always returns undefined.
✔ Does it change the original array?
❌ No, unless you manually modify items.
Example:
let fruits = ["Apple", "Mango", "Banana"];
fruits.forEach((fruit, index) => {
console.log(index, fruit);
});