
Wait — Do You Really Know JavaScript Arrays?
Before we dive in, let me throw a quick challenge at you. What does this code output?
let fruits = ['apple', 'banana', 'mango'];
fruits.favorite = 'orange';
fruits[5] = "mango";
console.log(fruits.length);
console.log(fruits);
Think about your answer first. I’ll explain the twist at the end. 😄
So What Even Is an Array?
Simple answer: an array is a way to store multiple values in one variable.
let fruits = ["apple", "banana", "orange"];
let mixed = ["apple", 123, false, { name: "xyz" }];
You can throw anything in there — strings, numbers, booleans, even objects. JavaScript doesn’t complain.
Here’s something that blew my mind when I first learned it: arrays are objects in JavaScript. That’s why they have built-in properties and methods. And that’s also why the code challenge above behaves the way it does — you can add custom properties to arrays just like objects!
Now let’s get to the fun part.
Warm-Up: The Everyday Methods, Told as a Shinchan Story
Before the less common methods, a quick refresher on the ten you’ll use every day. One fine morning, Shinchan realized his favorite Action Kamen toy was missing, so the gang started an investigation.
push() adds to the end. First, they made a list of clues:
let clues = [];
clues.push("Last seen in the garden");
clues.push("Himawari was near it");
clues.push("Misae vacuumed the house");
// ["Last seen in the garden", "Himawari was near it", "Misae vacuumed the house"]
pop() removes the last element. Kazama realized Misae vacuumed the day before, so the last clue was wrong:
clues.pop();
// ["Last seen in the garden", "Himawari was near it"]
shift() removes the first element. Nene pointed out that the garden clue was useless because it rained last night:
clues.shift();
// ["Himawari was near it"]
unshift() adds to the beginning. Then Bochan remembered seeing Shiro running with something, an important clue, so it goes first:
clues.unshift("Shiro might have taken it!");
// ["Shiro might have taken it!", "Himawari was near it"]
indexOf() finds a position, and includes() checks membership.
let suspects = ["Shiro", "Himawari", "Bochan"];
console.log(suspects.indexOf("Shiro")); // 0
console.log(suspects.includes("Himawari")); // true
slice() copies part of an array without changing the original.
let firstTwo = suspects.slice(0, 2);
console.log(firstTwo); // ["Shiro", "Himawari"]
console.log(suspects); // ["Shiro", "Himawari", "Bochan"] (unchanged)
filter() keeps the elements that pass a test. Bochan couldn’t get into Shinchan’s room, so he’s cleared:
let guilty = suspects.filter(name => name !== "Bochan");
// ["Shiro", "Himawari"]
map() turns each element into something new.
let plan = guilty.map(name => `${name} will be watched closely!`);
// ["Shiro will be watched closely!", "Himawari will be watched closely!"]
find() returns the first match. They spotted Himawari chewing something behind the sofa:
let culprit = guilty.find(name => name === "Himawari");
console.log(`${culprit} stole the toy!`); // "Himawari stole the toy!"
Case closed. Shinchan’s verdict: “Mom, justice has been served, but I demand a new toy!”
| Method | What it does | Changes the original array? |
|---|---|---|
push() / pop() |
Add / remove at the end | Yes |
unshift() / shift() |
Add / remove at the start | Yes |
indexOf() / includes() |
Find a position / check membership | No |
slice() |
Copy a portion | No |
filter() / map() |
New array from a test / a transform | No |
find() |
First matching element | No |
Now for the ones you probably haven’t used yet.
The 10 Array Methods Worth Knowing
#1 — copyWithin()
I’d never heard of this one until recently. It copies part of the array to another position within the same array — without changing its length.
A fun way to think about it: imagine a terminal command history that auto-shifts when you add a new command.
let history = [
"npm start",
"git status",
"git add .",
"git commit"
];
history.copyWithin(0, 1);
history[history.length - 1] = "git push";
console.log(history);
// ["git status", "git add .", "git commit", "git push"]
It “shifts everything left” and makes room for the new entry at the end. Honestly, weird at first — but kind of clever once you see it in action.
#2 — every()
This one checks if all elements pass a condition. Think of it like a quality gate in CI/CD.
const files = [
{ fileName: "login.js", coverage: 92 },
{ fileName: "signup.js", coverage: 88 },
{ fileName: "dashboard.js", coverage: 95 },
{ fileName: "profile.js", coverage: 90 }
];
const isCoverageGood = files.every(file => file.coverage >= 80);
console.log(isCoverageGood); // true
If even one file dips below 80%, it returns false. Super clean for validation logic.
#3 — reduce()
Okay, this one most developers have seen — but not everyone really gets it. The idea: take a whole array and boil it down to one value.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 15
Think of acc as your running total, and curr as the current item being processed. Once it clicked for me, I started using reduce() everywhere — for summing, grouping, building objects, you name it.
#4 — reduceRight()
Same idea as reduce(), but it starts from the right side of the array. Here’s a fun example:
const words = ["ChaiCode", "the", "to", "Welcome"];
const sentence = words.reduceRight((acc, curr) => acc + " " + curr);
console.log(sentence); // "Welcome to the ChaiCode"
The array is in reverse order, and reduceRight() assembles it correctly. I wish I’d known this earlier — I used to reverse arrays manually first. 🤦
#5 — some()
While every() checks if all elements pass, some() checks if at least one does. Great for role-based access checks.
const user = {
isLoggedIn: true,
roles: ["member", "editor"]
};
const requiredRoles = ["admin", "editor"];
const hasAccess = user.isLoggedIn && user.roles.some(role => requiredRoles.includes(role));
console.log(hasAccess); // true
The user has “editor” in their roles, and that matches one of the required roles — so access granted. Clean, readable, and practical.
#6 — with()
This is a newer one and it’s really handy. It returns a new array with one item replaced at a specific index — without mutating the original.
const students = ["Saurabh", "Hitesh", "Piyush"];
const updatedStudents = students.with(0, "Anirudh");
console.log(updatedStudents); // ["Anirudh", "Hitesh", "Piyush"]
console.log(students); // ["Saurabh", "Hitesh", "Piyush"] — unchanged!
When working with React state or any immutable data pattern, this is a lifesaver. No more spreading and remapping just to change one element.
#7 — shift()
This removes the first element from an array and returns it.
const numbers = [1, 2, 3, 4, 5];
const first = numbers.shift();
console.log(first); // 1
console.log(numbers); // [2, 3, 4, 5]
It does mutate the original array though, so use with care. Think of it like a queue — first in, first out.
#8 — unshift()
The opposite of shift() — it adds elements to the beginning of an array.
let presenters = ["Saurabh", "Pradip", "Yash"];
presenters.unshift("Rahul", "Amit");
console.log(presenters);
// ["Rahul", "Amit", "Saurabh", "Pradip", "Yash"]
Also mutates the original. I use this when I need to prepend items dynamically — like adding a “pinned” item to a list.
#9 — flatMap()
This one is a combo of map() and flat(). It maps over each element and then flattens the result by one level.
const numbers = [1, 2, [3, 4], 5];
const result = numbers.flatMap(n => Array.isArray(n) ? n : n);
console.log(result); // [1, 2, 3, 4, 5]
Where it really shines is when your map produces arrays and you don’t want nested arrays in the output. I used this while building a chat message parser — each message could expand into multiple UI elements, and flatMap() kept things clean.
#10 — includes()
Simple but essential. Checks if an array contains a specific value.
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(3)); // true
console.log(numbers.includes(6)); // false
Cleaner than using indexOf() !== -1 — which is what I used to do before I discovered this. 😅
The Challenge Answer
Remember the puzzle from the beginning?
let fruits = ['apple', 'banana', 'mango'];
fruits.favorite = 'orange';
fruits[5] = "mango";
console.log(fruits.length); // 6
console.log(fruits);
// ['apple', 'banana', 'mango', empty × 2, 'mango', favorite: 'orange']
The length is 6 because we set fruits[5] — JavaScript creates “empty” slots for the gaps. And fruits.favorite is a property on the array object, so it doesn’t affect length at all. Arrays in JavaScript are more flexible (and weird) than they look!
Key Takeaways
copyWithin()— shift data within the same arrayevery()/some()— check conditions across elementsreduce()/reduceRight()— collapse arrays into a single valuewith()— immutable single-element replacement (underrated!)shift()/unshift()— add/remove from the frontflatMap()— map + flatten in one stepincludes()— clean existence check



