JavaScript Array Programs
Work with lists of values in JavaScript. Find the largest item, sort, search, remove duplicates and use map, filter and reduce, with simple examples and output.
6 programs · open one, then the next
Start with Array Max & Min- 01Array Max & MinBeginnerFind the max and min of an array in JavaScript with Math.max(...arr), a loop, reduce and sort. Why empty arrays give -Infinity. Explanation and output.Open example →js// Method 1: Using Math.max and Math.min with spread operator let numbers = [10, 5, 20, 15, 8, 25]; let max = Math.max(...numbers);
- 02Array AverageBeginnerFind the average of an array in JavaScript with a loop, reduce and forEach. Learn to guard an empty array against divide by zero. Explanation and output.Open example →js// Method 1: Using for loop function averageLoop(arr) { if (arr.length === 0) return 0;
- 03Sum of Array ElementsBeginnerSum array elements in JavaScript with a for loop, reduce, forEach and for...of. Learn why reduce needs a 0 start value, with explanation and output.Open example →js// Method 1: Using for loop function sumArray(arr) { let sum = 0; for (let i = 0; i < arr.length; i++) {
- 04Reverse an ArrayBeginnerReverse an array in JavaScript with reverse(), a spread copy, loops and two pointers. Learn why reverse() changes the original. Explanation and output.Open example →js// Method 1: Using reverse() method (mutates original) let arr1 = [1, 2, 3, 4, 5]; arr1.reverse(); console.log("Using reverse():", arr1);
- 05Search in ArrayIntermediateSearch an array in JavaScript with indexOf, includes, find, findIndex, some and binary search. Why missing items give -1. Explanation and output.Open example →js// Method 1: Using indexOf let arr = [10, 20, 30, 40, 50]; let searchValue = 30;
- 06Array Methods: Filter, Map, ReduceIntermediateLearn JavaScript filter, map and reduce with simple examples and chaining. They build new arrays and keep the original, with explanation and output.Open example →jslet numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // FILTER: Creates new array with elements that pass test let evenNumbers = numbers.filter(num => num % 2 === 0);