JavaScript Core/
Lesson

Imagine you are building a shopping list app. Users add items one after another: milk, eggs, bread. An array in JavaScript works exactly like that list, it is an ordered collection where each item has a position (called an indexWhat is index?A data structure the database maintains alongside a table so it can find rows by specific columns quickly instead of scanning everything.) starting from zero. Arrays are the most fundamental data structure you will use in every project, from rendering lists of components in React to processing APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. responses.

Creating and accessing arrays

Arrays are created with square brackets, and items are separated by commas:

const fruits = ["apple", "banana", "cherry", "date"];
const numbers = [10, 20, 30, 40, 50];
const mixed = [1, "hello", true, null]; // arrays can hold any type
const empty = []; // empty array - perfectly valid

Access elements using bracket notation with the indexWhat is index?A data structure the database maintains alongside a table so it can find rows by specific columns quickly instead of scanning everything.:

const fruits = ["apple", "banana", "cherry"];

console.log(fruits[0]);  // "apple" (first item)
console.log(fruits[1]);  // "banana" (second item)
console.log(fruits[2]);  // "cherry" (third item)
console.log(fruits[5]);  // undefined (no item at that index)
IndexValueHow to access
0"apple"fruits[0]
1"banana"fruits[1]
2"cherry"fruits[2]
lastdependsfruits[fruits.length - 1]
last (modern)dependsfruits.at(-1)
Zero-based indexing
Programming languages start counting at 0, not 1. The first item is at index 0, the second at index 1, and so on. It takes some getting used to, but you will internalize it quickly.
02

Finding array length

Every array has a .length property telling you how many items it contains:

const fruits = ["apple", "banana", "cherry"];
console.log(fruits.length);  // 3

// Get the last element safely
const lastFruit = fruits[fruits.length - 1];  // "cherry"

// Modern alternative: .at() accepts negative indices
const alsoLast = fruits.at(-1);  // "cherry"

The .length property is live, it updates automatically when you add or remove elements. This matters because it means you should never cache .length and assume it stays the same after modifying the array.

03

Adding and removing elements

JavaScript provides several methods to modify arrays. These methods mutate (change) the original array:

MethodWhat it doesReturnsMutates original?
push(item)Adds to endNew lengthYes
pop()Removes from endRemoved itemYes
unshift(item)Adds to startNew lengthYes
shift()Removes from startRemoved itemYes
splice(i, n)Removes n items at index iRemoved itemsYes
const tasks = ["wash dishes"];

tasks.push("cook dinner");      // ["wash dishes", "cook dinner"]
const done = tasks.pop();       // done = "cook dinner", tasks = ["wash dishes"]
tasks.unshift("buy groceries"); // ["buy groceries", "wash dishes"]
const first = tasks.shift();    // first = "buy groceries", tasks = ["wash dishes"]
04

Checking if an item exists

Use includes() to check if an array contains a specific value, and indexOf() to find where it sits:

const fruits = ["apple", "banana", "cherry"];

console.log(fruits.includes("banana"));  // true
console.log(fruits.includes("grape"));   // false

// Find the index of an item
console.log(fruits.indexOf("banana"));   // 1
console.log(fruits.indexOf("grape"));    // -1 (not found)

Note that includes() uses strict equality (===), so [1, 2, 3].includes("1") returns false, the string "1" is not the number 1.

05

Modifying arrays in place

You can change elements directly by their indexWhat is index?A data structure the database maintains alongside a table so it can find rows by specific columns quickly instead of scanning everything.:

const colors = ["red", "green", "blue"];
colors[1] = "yellow";  // ["red", "yellow", "blue"]

Or use splice() to add, remove, or replace elements at any position:

const numbers = [1, 2, 3, 4, 5];

// Remove 2 elements starting at index 1
numbers.splice(1, 2);  // numbers is now [1, 4, 5]

// Insert elements at index 1 without removing any
numbers.splice(1, 0, 2, 3);  // numbers is now [1, 2, 3, 4, 5]
AI pitfall, mutation surprises
AI tools love using push() and splice() to modify arrays directly, even when immutability is safer. In React state, Redux stores, or any shared data, mutating an array causes subtle bugs because React compares references to detect changes. If AI generates items.push(newItem), ask yourself whether you should use const updated = [...items, newItem] instead. Similarly, prefer items.filter(x => x.id !== targetId) over items.splice(index, 1) for removals. Always use immutable patterns when the array is shared or part of application state.
06

Arrays are objects (with superpowers)

Under the hood, arrays are actually objects with numeric keys. But they are special objects optimized for sequential data:

const arr = ["a", "b", "c"];

console.log(typeof arr);          // "object"
console.log(Array.isArray(arr));   // true

Use Array.isArray() to check if something is an array. Do not rely on typeof, it returns "object" for arrays, plain objects, and null, which is not helpful.

Best practice
Use arrays for ordered lists and objects for named properties. Do not mix the paradigms, it makes your code harder to understand.
07

Quick reference

OperationMethod / SyntaxMutates?
Create[1, 2, 3],
Readarr[0]No
Read lastarr.at(-1)No
Add to endarr.push(x)Yes
Remove from endarr.pop()Yes
Add to startarr.unshift(x)Yes
Remove from startarr.shift()Yes
Check existencearr.includes(x)No
Find indexarr.indexOf(x)No
Get lengtharr.lengthNo
Immutable add[...arr, x]No
Check typeArray.isArray(arr)No