An array is a collection of elements stored in a single variable.
let fruits = ["apple", "banana", "mango"];
🔹 Categories of Array Methods
To make things easier, we’ll group methods into:
- Basic / Access Methods
- Add / Remove Elements
- Iteration Methods
- Transformation Methods
- Search / Find Methods
- Utility Methods
🔹 1. Basic / Access Methods
✅ length
Returns number of elements
let arr = [1, 2, 3];
console.log(arr.length); // 3
✅ toString()
Converts array to string
let arr = [1, 2, 3];
console.log(arr.toString()); // "1,2,3"
✅ at()
Access element using index (supports negative index)
let arr = [10, 20, 30];
console.log(arr.at(-1)); // 30
🔹 2. Add / Remove Elements
✅ push()
Adds element at end
let arr = [1, 2];
arr.push(3); // [1,2,3]
✅ pop()
Removes last element
arr.pop(); // removes 3
✅ unshift()
Adds element at beginning
arr.unshift(0); // [0,1,2]
✅ shift()
Removes first element
arr.shift(); // removes 0
✅ splice()
Add/remove elements at specific index
let arr = [1, 2, 3, 4];
arr.splice(1, 2); // removes 2,3 → [1,4]
🔹 3. Iteration Methods
✅ forEach()
Executes function for each element
arr.forEach((item) => console.log(item));
✅ map()
Returns new array after transformation
let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2);
// [2,4,6]
✅ filter()
Returns elements that match condition
let nums = [1, 2, 3, 4];
let even = nums.filter(n => n % 2 === 0);
// [2,4]
✅ reduce()
Reduces array to single value
let nums = [1, 2, 3];
let sum = nums.reduce((acc, n) => acc + n, 0);
// 6
🔹 4. Transformation Methods
✅ concat()
Merges arrays
let a = [1,2];
let b = [3,4];
let result = a.concat(b); // [1,2,3,4]
✅ slice()
Returns shallow copy
let arr = [1,2,3,4];
arr.slice(1,3); // [2,3]
✅ flat()
Flattens nested arrays
let arr = [1, [2, [3]]];
arr.flat(2); // [1,2,3]
🔹 5. Search / Find Methods
✅ indexOf()
Returns first index
let arr = [10, 20, 30];
arr.indexOf(20); // 1
✅ includes()
Checks existence
arr.includes(30); // true
✅ find()
Returns first matching element
let users = [{id:1}, {id:2}];
let user = users.find(u => u.id === 2);
✅ findIndex()
Returns index of matching element
users.findIndex(u => u.id === 2); // 1
🔹 6. Utility Methods
✅ sort()
Sorts array (mutates original)
let arr = [3,1,2];
arr.sort(); // [1,2,3]
⚠️ For numbers:
arr.sort((a,b) => a - b);
✅ reverse()
Reverses array
arr.reverse();
✅ join()
Converts to string with separator
let arr = ["a", "b", "c"];
arr.join("-"); // "a-b-c"
15 JavaScript Array Programs with Solutions
1. Find Largest Number in Array
Solution
const numbers = [10, 45, 2, 99, 23];
const largest = Math.max(...numbers);
console.log(largest);
Output
99
2. Find Smallest Number in Array
Solution
const numbers = [10, 45, 2, 99, 23];
const smallest = Math.min(...numbers);
console.log(smallest);
Output
2
3. Sum of Array Elements
Solution
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((total, num) => total + num, 0);
console.log(sum);
Output
15
4. Find Even Numbers
Solution
const numbers = [1, 2, 3, 4, 5, 6];
const even = numbers.filter(num => num % 2 === 0);
console.log(even);
Output
[2, 4, 6]
5. Find Odd Numbers
Solution
const numbers = [1, 2, 3, 4, 5, 6];
const odd = numbers.filter(num => num % 2 !== 0);
console.log(odd);
Output
[1, 3, 5]
6. Reverse an Array
Solution
const arr = [1, 2, 3, 4, 5];
const reversed = arr.reverse();
console.log(reversed);
Output
[5, 4, 3, 2, 1]
7. Remove Duplicate Elements
Solution
const arr = [1, 2, 2, 3, 4, 4, 5];
const unique = [...new Set(arr)];
console.log(unique);
Output
[1, 2, 3, 4, 5]
8. Sort Array in Ascending Order
Solution
const numbers = [40, 5, 100, 25];
numbers.sort((a, b) => a - b);
console.log(numbers);
Output
[5, 25, 40, 100]
9. Sort Array in Descending Order
Solution
const numbers = [40, 5, 100, 25];
numbers.sort((a, b) => b - a);
console.log(numbers);
Output
[100, 40, 25, 5]
10. Find Maximum and Minimum Together
Solution
const arr = [10, 20, 5, 90, 30];
const max = Math.max(...arr);
const min = Math.min(...arr);
console.log("Max:", max);
console.log("Min:", min);
Output
Max: 90
Min: 5
11. Merge Two Arrays
Solution
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const merged = [...arr1, ...arr2];
console.log(merged);
Output
[1, 2, 3, 4, 5, 6]
12. Check if Element Exists
Solution
const fruits = ["apple", "banana", "mango"];
console.log(fruits.includes("banana"));
console.log(fruits.includes("orange"));
Output
true
false
13. Find Index of Element
Solution
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);
console.log(index);
Output
2
14. Convert Array to String
Solution
const fruits = ["apple", "banana", "mango"];
const result = fruits.join(", ");
console.log(result);
Output
apple, banana, mango
15. Find Second Largest Number
Solution
const arr = [10, 50, 20, 80, 60];
const sorted = [...arr].sort((a, b) => b - a);
console.log(sorted[1]);
Output
60
Bonus Program — Flatten Nested Array
Solution
const arr = [1, [2, 3], [4, [5, 6]]];
const flatArray = arr.flat(2);
console.log(flatArray);
Output
[1, 2, 3, 4, 5, 6]
Topics Covered
These programs cover:
map()filter()reduce()sort()includes()indexOf()join()- Spread operator
- Set
- Array manipulation
- Searching
- Sorting
- Flattening arrays