- filter()
- some()
- every()
- map()
- forEach()
- reduce()
- indexOf()
Ans.
1. filter()
- Creates a new array with elements that pass a test (returns
true
). - Doesn’t change the original array.
let numbers = [10, 25, 30, 45];
let result = numbers.filter(num => num > 20);
console.log(result); // [25, 30, 45]
2. some()
- Checks if at least one element passes a condition.
- Returns
true
orfalse
.
let nums = [3, 6, 8];
console.log(nums.some(num => num > 5)); // true
3. every()
- Checks if all elements satisfy a condition.
- Returns
true
orfalse
.
let nums2 = [2, 4, 6];
console.log(nums2.every(num => num % 2 === 0)); // true
4. map()
- Creates a new array by applying a function to each element.
- Doesn’t change the original array.
let arr = [1, 2, 3];
let doubled = arr.map(x => x * 2);
console.log(doubled); // [2, 4, 6]
5. forEach()
- Executes a function once for each array element.
- Does not return a new array.
let fruits = ["apple", "banana", "cherry"];
fruits.forEach(fruit => console.log(fruit.toUpperCase()));
// APPLE
// BANANA
// CHERRY
6. reduce()
- Reduces the array to a single value (like sum, product).
- Takes a callback with an accumulator and current value.
let numbers2 = [1, 2, 3, 4];
let sum = numbers2.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 10
7. indexOf()
- Returns the first index of a value in the array.
- Returns
-1
if not found.
let colors = ["red", "green", "blue", "green"];
console.log(colors.indexOf("green")); // 1
console.log(colors.indexOf("yellow")); // -1