JavaScript Arrays: Everything from Basics to Magic Tricks 🎩✨

Raveena PuttaRaveena Putta
4 min read

When I first started coding, someone told me “Just put it in an array.”
I nodded like I understood… but inside I was thinking — what on Earth is an array? A food tray? 🍱

Spoiler: Arrays are just lists. That’s it.
They’re like a row of lockers 🏫 — each locker has a number (index) and inside it, you can store something: names, numbers, even other arrays.


1. What is an Array? 🤔

An array is an ordered list of items.
It’s like a shelf with boxes — each box has a number starting from 0 (yes, programmers start counting at zero, don’t fight me on this 😅).

let fruits = ["Apple", "Banana", "Cherry"];

📦 In your head:

  • Locker 0 → "Apple"

  • Locker 1 → "Banana"

  • Locker 2 → "Cherry"


2. Creating Arrays 🛠

The classic way

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

Using the Array constructor

let colors = new Array("Red", "Green", "Blue");

💡 Tip: Most of the time, just use [ ]. It’s shorter and cleaner.


3. Accessing Array Items 🔍

We use indexes (positions) to get stuff.

let snacks = ["Chips", "Cookies", "Popcorn"];
console.log(snacks[0]); // Chips
console.log(snacks[2]); // Popcorn

💡 Analogy: Just like locker numbers, snacks[0] means “open locker number 0.”


4. Changing and Adding Items 🖊

let pets = ["Dog", "Cat"];
pets[1] = "Parrot"; // Change Cat → Parrot
pets[2] = "Hamster"; // Add new item
console.log(pets); // ["Dog", "Parrot", "Hamster"]

5. Array Length 📏

let playlist = ["Song1", "Song2", "Song3"];
console.log(playlist.length); // 3

💡 Magic Trick: .length is not just to check — you can also use it to add things!

playlist[playlist.length] = "Song4";

6. Common Array Methods 🪄

Push & Pop — End of the array

let cart = ["Milk"];
cart.push("Bread"); // Add at the end
cart.pop(); // Remove last

💡 Push is adding a new box at the end. Pop is removing the last box.


Shift & Unshift — Start of the array

let line = ["John", "Mary"];
line.unshift("Alex"); // Add at the start
line.shift(); // Remove from start

💡 Unshift is like letting someone cut in front of the line. Shift is sending the first person away.


indexOf & includes — Finding stuff

let guests = ["Anna", "Bob", "Clara"];
console.log(guests.indexOf("Bob")); // 1
console.log(guests.includes("Zara")); // false

Slice & Splice — Cutting stuff

let colors = ["Red", "Green", "Blue", "Yellow"];

// slice(start, end) — makes a copy
console.log(colors.slice(1, 3)); // ["Green", "Blue"]

// splice(start, count) — removes/changes
colors.splice(2, 1); // Removes "Blue"

map, filter, reduce — The magic trio 🎩

map — transforms each item

let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2); // [2, 4, 6]

filter — keeps only what matches

let ages = [15, 18, 21, 12];
let adults = ages.filter(a => a >= 18); // [18, 21]

reduce — makes one value

let prices = [5, 10, 15];
let total = prices.reduce((sum, p) => sum + p, 0); // 30

💡 These are like factory workers:

  • map changes every product

  • filter throws away some products

  • reduce puts everything into one box


7. Arrays Can Hold Anything 🧠

let randomStuff = [
  "Hello", 
  42, 
  true, 
  { name: "Raveena" }, 
  [1, 2, 3]
];

Yes — even arrays inside arrays (that’s called a nested array).


8. Some Quick Magic Tricks ✨

  • Reverse an array
let arr = [1, 2, 3];
arr.reverse(); // [3, 2, 1]
  • Join items into a string
let words = ["I", "love", "JavaScript"];
console.log(words.join(" ")); // I love JavaScript
  • Spread items
let a = [1, 2];
let b = [3, 4];
let merged = [...a, ...b]; // [1, 2, 3, 4]

Final Takeaways 🎯

  • Arrays are just lists with numbered lockers

  • They start counting from 0 (don’t ask, it’s just tradition 😆)

  • You can add, remove, find, slice, and even transform them

  • Some methods are pure magic — use them to save lines of code

Once you see arrays as rows of lockers or shelves of boxes, they’ll stop feeling mysterious and start feeling like your best coding friend.

0
Subscribe to my newsletter

Read articles from Raveena Putta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raveena Putta
Raveena Putta