JS Basics
JavaScript Arrays
Complete guide to JavaScript arrays: creation, methods, iteration, and functional patterns.
JavaScript Arrays
An array is a special variable, which can hold more than one value. Arrays are zero-indexed — the first element is at index 0.
Array Methods
JavaScript arrays have many powerful built-in methods:
Mutation methods (modify original):
push(),pop(),shift(),unshift()splice(),sort(),reverse()
Iteration methods (non-mutating):
map(),filter(),reduce(),forEach()find(),findIndex(),some(),every()flat(),flatMap()
Modern Array Methods
ES6+ added powerful array methods that enable clean functional programming patterns.
Example
javascript
// Array creation
const fruits = ["apple", "banana", "cherry"];
const numbers = Array.from({length: 5}, (_, i) => i + 1);
// [1, 2, 3, 4, 5]
// Basic operations
fruits.push("date"); // add to end
fruits.pop(); // remove from end
fruits.unshift("avocado"); // add to start
fruits.shift(); // remove from start
// Powerful array methods
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// map - transform each element
const squared = data.map(n => n ** 2);
// filter - keep elements matching condition
const evens = data.filter(n => n % 2 === 0);
// reduce - accumulate to single value
const sum = data.reduce((acc, n) => acc + n, 0);
// find - first matching element
const firstBig = data.find(n => n > 5); // 6
// some / every
const hasNegative = data.some(n => n < 0); // false
const allPositive = data.every(n => n > 0); // true
// Chaining
const result = data
.filter(n => n % 2 === 0)
.map(n => n * 3)
.reduce((acc, n) => acc + n, 0);
console.log(result); // 90
// Spread / destructuring
const [first, ...rest] = data;
const combined = [...fruits, "elderberry"];Try it yourself — JAVASCRIPT