
Array Methods
Array methods are built-in functions that allow you to perform operations on arrays in a concise and efficient way. They provide a powerful set of tools for manipulating and working with arrays without the need for manual loops.
Here are some of the most commonly used array methods in JavaScript:
1. map():
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(number => number * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]2. filter():
const ages = [18, 25, 16, 30, 20];
const adults = ages.filter(age => age >= 18);
console.log(adults); // Output: [18, 25, 30, 20] 3. forEach():
const names = ["Alice", "Bob", "Charlie"];
names.forEach(name => console.log(name));4. sort():
const fruits = ["banana", "apple", "orange"];
fruits.sort();
console.log(fruits); // Output: ["apple", "banana", "orange"]5. join():
const fruits = ["Apple", "Banana", "Cherry"];
const result = fruits.join(" - ");
console.log(result); // Output: "Apple - Banana - Cherry"6. slice():
const numbers = [1, 2, 3, 4, 5];
const sliced = numbers.slice(1, 3);
console.log(sliced); // Output: [2, 3]7. concat():
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const combined = array1.concat(array2);
console.log(combined); // Output: [1, 2, 3, 4, 5, 6]8. includes():
const numbers = [1, 2, 3, 4, 5];
const includesThree = numbers.includes(3);
console.log(includesThree); // Output: true9. push():
const numbers = [1, 2, 3];
numbers.push(4, 5);
console.log(numbers); // Output: [1, 2, 3, 4, 5]10. pop():
const numbers = [1, 2, 3];
const last = numbers.pop();
console.log(numbers); // Output: [1, 2]
console.log(last); // Output: 3