Free Online Code Editor
Array methods: map, filter, reduce, and more.
// Creating arrays
const fruits = ["apple", "banana", "cherry"];
const numbers = [1, 2, 3, 4, 5];
// Accessing elements
console.log(fruits[0]); // apple
console.log(fruits[fruits.length - 1]); // cherry
// Adding/removing elements
fruits.push("date"); // Add to end
fruits.pop(); // Remove from end
fruits.unshift("apricot"); // Add to start
fruits.shift(); // Remove from start
console.log(fruits);
// Finding elements
console.log(fruits.includes("banana")); // true
console.log(fruits.indexOf("cherry")); // 2
// map - transform each element
const doubled = numbers.map(n => n * 2);
console.log(doubled);
// filter - keep elements that match condition
const evens = numbers.filter(n => n % 2 === 0);
console.log(evens);
// reduce - combine into single value
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(sum);
// find - first element that matches
const found = numbers.find(n => n > 3);
console.log(found);
// some/every - check if any/all match
console.log(numbers.some(n => n > 5)); // false
console.log(numbers.every(n => n > 0)); // true
// forEach - iterate (no return)
numbers.forEach(n => console.log(n * 2));
// Chaining methods
const result = numbers
.filter(n => n % 2 === 0)
.map(n => n * 10)
.reduce((sum, n) => sum + n, 0);
console.log(result);Arrays have powerful methods. map/filter/reduce are essential. Chaining methods creates readable data transformations.