Arrays
Arrays in JavaScript
Arrays are ordered collections of values. They can hold multiple values in a single variable and are zero-indexed.
Creating Arrays
const fruits = ["apple", "banana", "orange"];
const numbers = [1, 2, 3, 4, 5];
const mixed = [1, "two", true, null];
Accessing Array Elements
const colors = ["red", "green", "blue"];
console.log(colors[0]); // Output: red
console.log(colors[1]); // Output: green
console.log(colors.length); // Output: 3
Array Methods
JavaScript arrays have many built-in methods:
const numbers = [1, 2, 3, 4, 5];
// push - add to end
numbers.push(6); // [1, 2, 3, 4, 5, 6]
// pop - remove from end
numbers.pop(); // [1, 2, 3, 4, 5]
// map - transform each element
const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10]
// filter - keep elements matching condition
const evens = numbers.filter(n => n % 2 === 0); // [2, 4]
// reduce - accumulate to single value
const sum = numbers.reduce((acc, n) => acc + n, 0); // 15
Iterating Over Arrays
const names = ["Alice", "Bob", "Charlie"];
// for...of loop
for (const name of names) {
console.log(name);
}
// forEach method
names.forEach(name => console.log(name));