Track: JavaScript Basics
Lesson: JS-08 / JS-10
๐ฏ Lesson Goal
By the end of this lesson, you will be able to use modern JavaScript array methods such as forEach, map, filter, and reduce to process data efficiently and write clean, professional code used in real-world applications.
๐ Prerequisites
- JS-01: Variables and Data Types
- JS-02: Operators and Expressions
- JS-03: Conditional Statements
- JS-04: Loops
- JS-05: Functions
- JS-06: Arrays
- JS-07: Objects
You should be comfortable with:
- Arrays
- Functions
- Objects
๐ Why Array Methods Are Important
Modern JavaScript avoids manual loops whenever possible.
Array methods:
- Make code shorter and cleaner
- Improve readability
- Are heavily used in React, Node, APIs, MERN
- Reduce bugs
If you know map, filter, and reduce, you are already thinking like a professional developer.
๐น forEach() โ Loop Through an Array
Executes a function for each element.
Syntax
array.forEach(function(item) {
// code
});
Example
let numbers = [10, 20, 30];
numbers.forEach(function(num) {
console.log(num);
});
โ Used when you want to perform an action, not return data.
๐น map() โ Transform Data
Creates a new array by modifying each element.
Example
let numbers = [1, 2, 3, 4];
let squares = numbers.map(function(num) {
return num * num;
});
console.log(squares);
โ Output:
[1, 4, 9, 16]
map() with Arrow Function
let prices = [100, 200, 300];
let updatedPrices = prices.map(price => price + 50);
๐น filter() โ Select Data
Returns a new array containing elements that satisfy a condition.
Example
let marks = [35, 78, 90, 45, 60];
let passed = marks.filter(mark => mark >= 40);
console.log(passed);
โ Output:
[78, 90, 45, 60]
๐น reduce() โ Combine Data (Very Important)
Used to reduce an array to a single value.
Syntax
array.reduce(function(accumulator, currentValue) {
return updatedValue;
}, initialValue);
Example: Sum of numbers
let numbers = [10, 20, 30];
let total = numbers.reduce(function(sum, num) {
return sum + num;
}, 0);
console.log(total);
โ Output:
60
reduce() with Arrow Function
let total = numbers.reduce((sum, num) => sum + num, 0);
๐น Real-World Example (Objects + Methods)
let students = [
{ name: "Aman", marks: 85 },
{ name: "Riya", marks: 92 },
{ name: "Karan", marks: 38 }
];
Get names of passed students
let passedNames = students
.filter(student => student.marks >= 40)
.map(student => student.name);
console.log(passedNames);
โ This method chaining is very common in React & MERN.
๐น find() โ Find One Element
Returns the first matching element.
let topper = students.find(student => student.marks > 90);
console.log(topper);
๐น some() and every()
some() โ checks if any element satisfies condition
let hasFail = students.some(student => student.marks < 40);
every() โ checks if all elements satisfy condition
let allPassed = students.every(student => student.marks >= 40);
โ ๏ธ Common Mistakes
- Expecting
forEach()to return a value - Forgetting
returninsidemap()orreduce() - Modifying original array unintentionally
- Confusing
map()withfilter()
๐ง Practice Tasks
- Use
map()to double all numbers in an array - Use
filter()to get even numbers - Use
reduce()to calculate total marks - Use
find()to get a specific object - Chain
filter()andmap()together
๐งช Mini Coding Challenge
let orders = [
{ id: 1, amount: 1200 },
{ id: 2, amount: 450 },
{ id: 3, amount: 800 }
];
Tasks:
- Get orders above 500
- Extract their amounts
- Calculate total amount using
reduce()
๐ Summary
forEach()โ perform actionmap()โ transform datafilter()โ select datareduce()โ combine data- Method chaining is powerful and professional
โก๏ธ Next Lesson
JavaScript DOM Manipulation (JS-09)
(Connecting JavaScript with HTML)
๐ Quiz & Badge
Quiz and badges will be enabled soon.