NelsonLabs

Arrays

Arrays are ordered lists of values. They're fundamental to JavaScript — you'll use them everywhere, from rendering lists in React to processing API responses.

Creating and accessing arrays
javascript
// Creating arrays
const fruits  = ["apple", "banana", "mango"];
const numbers = [1, 2, 3, 4, 5];
const mixed   = ["text", 42, true, null];  // valid but avoid mixed types

// Accessing elements — index starts at 0
fruits[0]   // "apple"
fruits[1]   // "banana"
fruits[fruits.length - 1]  // "mango" — last element

// Destructuring
const [first, second, ...rest] = fruits;
// first = "apple", second = "banana", rest = ["mango"]
The essential array methods
javascript
const courses = ["HTML", "CSS", "JavaScript", "React", "Next.js"];

// Adding and removing
courses.push("Python");          // add to end → mutates
courses.pop();                   // remove from end → returns removed item
courses.unshift("Beginner");     // add to start → mutates
courses.shift();                 // remove from start → returns removed item

// Finding
courses.includes("React");       // true
courses.indexOf("CSS");          // 1
courses.find(c => c.length > 5); // "JavaScript" (first match)
courses.findIndex(c => c === "React"); // 3

// Transforming — these return NEW arrays (non-mutating)
courses.map(c => c.toLowerCase());
// ["html", "css", "javascript", "react", "next.js"]

courses.filter(c => c.length <= 4);
// ["HTML", "CSS"]

// Reducing to a single value
const totalLength = courses.reduce((sum, c) => sum + c.length, 0);

// Slicing and joining
courses.slice(1, 3);    // ["CSS", "JavaScript"] (doesn't mutate)
courses.join(", ");     // "HTML, CSS, JavaScript, React, Next.js"

ANALOGY

map, filter, and reduce. Think of an array as a bag of items going through a factory. map transforms every item (changes the shape but keeps the count). filter is a quality check (removes items that don't pass). reduce combines everything into one result (like a summary report). These three methods handle 80% of data processing tasks.