Classic JS Tutorial | JS in VS 2026 | JS Examples | jQuery
⚡ Lesson 07 of 30

Arrays

Create, read, mutate, and transform arrays using modern array methods.

Creating Arrays

Arrays hold ordered lists of values of any type:

const fruits = ["apple", "banana", "cherry"];
const mixed  = [1, "two", true, null];
const matrix = [[1,2],[3,4]]; // nested arrays
console.log(fruits[0]); // "apple"
console.log(fruits.length); // 3

Adding and Removing Elements

The most common mutation methods:

const arr = [1, 2, 3];
arr.push(4);      // [1,2,3,4] — add to end
arr.pop();        // [1,2,3]   — remove from end
arr.unshift(0);   // [0,1,2,3] — add to start
arr.shift();      // [1,2,3]   — remove from start
arr.splice(1,1);  // [1,3]     — remove at index 1

Searching & Slicing

const nums = [10, 20, 30, 40, 50];
console.log(nums.indexOf(30));         // 2
console.log(nums.includes(40));        // true
console.log(nums.slice(1, 3));         // [20, 30]
console.log(nums.find(n => n > 25));   // 30
console.log(nums.findIndex(n=>n>25));  // 2

Transforming with map, filter, reduce

These three methods are the backbone of functional array programming:

const prices = [100, 200, 300, 400];

const discounted = prices.map(p => p * 0.9);
// [90, 180, 270, 360]

const expensive = prices.filter(p => p >= 200);
// [200, 300, 400]

const total = prices.reduce((sum, p) => sum + p, 0);
// 1000

Sorting and Flattening

const names = ["Charlie","Alice","Bob"];
names.sort(); // ["Alice","Bob","Charlie"]

const nested = [1,[2,3],[4,[5]]];
console.log(nested.flat());    // [1,2,3,4,[5]]
console.log(nested.flat(2));   // [1,2,3,4,5]
← Lesson 06🏠 HomeLesson 08 →