These methods replace most hand-written for loops, and they make your intent clear.
const students = [
{ name: 'Asha', branch: 'CSE', cgpa: 8.7 },
{ name: 'Ravi', branch: 'ECE', cgpa: 7.4 },
{ name: 'Meena', branch: 'CSE', cgpa: 9.1 }
];
const names = students.map(s => s.name);
const cse = students.filter(s => s.branch === 'CSE');
const avg = students.reduce((sum, s) => sum + s.cgpa, 0) / students.length;
const topper = students.find(s => s.cgpa > 9);
console.log(names.join(', '));
console.log(cse.length, avg.toFixed(2), topper.name);
console.log(students.some(s => s.cgpa < 7.5), students.every(s => s.cgpa >= 7));Asha, Ravi, Meena
2 8.40 Meena
true true- map: same length, each item transformed.
- filter: only the items that pass a test.
- reduce: folds everything into one value. Always give it a starting value (0 above).
- find: the first match, or undefined. findIndex gives its position.
- some / every: does at least one, or does every item, pass the test.
The sort mistake
Without a compare function, sort turns items into strings and compares them as text. It also changes the original array.
const marks = [100, 25, 9, 70];
console.log([...marks].sort().join(' '));
console.log([...marks].sort((a, b) => a - b).join(' '));
console.log(marks.join(' '));100 25 70 9
9 25 70 100
100 25 9 70Copy the array first ([...marks]) when you need the original order later. Newer engines also offer toSorted(), which returns a sorted copy.
Chaining
Because map and filter return new arrays, you can chain them: students.filter(s => s.branch === 'CSE').map(s => s.name) gives the CSE names in one readable line.
#javascript#arrays#map filter reduce#sort
Sources and further reading
Written by the UniCircle team, checked by an editor on 22 Oct 2026. It is not official policy of any university. Exam patterns and rules change; confirm dates and rules on the official website.
