Home/Concepts/Arrays
📋
Intermediate⏱ 14 min#05 / 08

Arrays

Lists & Array Methods

Arrays are ordered collections that store multiple values in one variable. JavaScript arrays come with powerful built-in methods for adding, removing, searching, sorting, and transforming data.

Your Progress0/8 complete
0 visited0%
💡 Real-World Analogy

An array is like a movie playlist. Add to end (push), remove from end (pop), add to start (unshift), remove from start (shift). Transform every title (map), keep only certain genres (filter), calculate total runtime (reduce).

🔑 Key Concepts

Array Basics

Create, read, and modify using 0-based indexes. push, pop, shift, unshift for adding/removing.

map / filter / reduce

The power trio: map transforms, filter keeps matches, reduce accumulates to one value.

Search & Sort

find, findIndex, includes, indexOf, some, every, sort, reverse - find and order elements.

💻 Code Examples

1Array Basics
arrays.js
1// Creating arrays
2let fruits = ["Apple", "Banana", "Cherry"];
3let numbers = [1, 2, 3, 4, 5];
4let mixed = ["hello", 42, true, null];
5 
6// Access (0-indexed)
7fruits[0]; // "Apple"
8fruits[fruits.length - 1]; // "Cherry" (last)
9 
10// Modify
11fruits.push("Mango"); // add to END
12fruits.pop(); // remove from END
13fruits.unshift("Avocado"); // add to START
14fruits.shift(); // remove from START
15fruits[1] = "Blueberry"; // replace by index
16 
17console.log(fruits.length); // count items
2map, filter & reduce
arrays.js
1let scores = [85, 42, 91, 67, 55, 98];
2 
3// MAP - transform every element into new array
4let doubled = scores.map(s => s * 2);
5// [170, 84, 182, 134, 110, 196]
6 
7let letters = scores.map(s =>
8 s >= 90 ? "A" : s >= 70 ? "B" : "C"
9);
10// ["B", "C", "A", "B", "C", "A"]
11 
12// FILTER - keep elements that pass the test
13let passing = scores.filter(s => s >= 70);
14// [85, 91, 98]
15 
16// REDUCE - accumulate to a single value
17let total = scores.reduce((acc, s) => acc + s, 0);
18// 438
19let average = total / scores.length; // 73
3Search, Sort & More
arrays.js
1let students = ["Alice", "Bob", "Charlie", "Diana"];
2 
3// Searching
4students.includes("Bob"); // true
5students.indexOf("Charlie"); // 2
6students.find(s => s.length > 5); // "Charlie"
7students.findIndex(s => s === "Diana"); // 3
8students.some(s => s === "Eve"); // false
9students.every(s => s.length > 2); // true
10 
11// Sorting
12let nums = [3, 1, 4, 1, 5, 9, 2, 6];
13nums.sort((a, b) => a - b); // ascending
14nums.reverse(); // flip order
15 
16// Slicing & Spreading
17students.slice(1, 3); // ["Bob", "Charlie"]
18let combined = [...nums, ...students]; // merge arrays

⚠️ Common Mistakes

❌ Mistake

Arrays start at index 1

✅ Fix

Arrays are 0-indexed! First element is index 0, last is array.length - 1.

❌ Mistake

map/filter change the original array

✅ Fix

These are NON-DESTRUCTIVE - they return a NEW array. Original stays the same.

❌ Mistake

.sort() without comparator on numbers

✅ Fix

[10, 9, 100].sort() gives [10, 100, 9] (wrong!). Always use .sort((a,b) => a-b) for numbers.

📋 Quick Reference

.push() / .pop().shift() / .unshift().slice(start, end).splice(i, count).map(fn).filter(fn).reduce(fn, init).find(fn).sort((a,b) => a-b).join(sep).includes(val)

Live Code Playground

Edit the code and press ▶ Run to see output instantly.

arrays_playground.js
⟳ Loading editor...
💾 Auto-saved
// Press ▶ Run Code to execute and view output console logs...
🔄
← Previous
Loops
Challenge Mode
Next →
Functions
⚙️