Skip to main content

Command Palette

Search for a command to run...

JavaScript Array Methods with a Shinchan Story!

Updated
β€’3 min read
JavaScript Array Methods with a Shinchan Story!

🎭 Story: "Shinchan and the Missing Toy Mystery"

One fine morning, Shinchan was playing in his room when he realized his favorite "Action Kamen" toy was missing! 😱

1️⃣ push() – Adding Clues to the List

First, they created 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");

πŸ’‘ Explanation: The push() method adds elements to the end of an array.


2️⃣ pop() – Removing Wrong Clues

Kazama, the "Detective", realized one clue was wrongβ€”Misae vacuumed the day before, not today!

clues.pop(); // Remove the last clue

πŸ’‘ Explanation: The pop() method removes the last element from an array.


3️⃣ unshift() – Adding an Important Clue to the Beginning

Bochan suddenly remembered:
"We saw Shiro running with something last evening!"
So they added this clue at the beginning:

clues.unshift("Shiro might have taken it!");

πŸ’‘ Explanation: The unshift() method adds elements to the beginning of an array.


4️⃣ shift() – Removing an Unnecessary Clue

Nene pointed out that the garden clue was useless because it rained last night!

clues.shift(); // Remove first clue

πŸ’‘ Explanation: The shift() method removes the first element from an array.


5️⃣ indexOf() – Finding Suspects

They created a suspect list based on who was near the toy:

let suspects = ["Shiro", "Himawari", "Bochan"];

Kazama asked, "Is Shiro on the list?"

console.log(suspects.indexOf("Shiro")); // Output: 0

πŸ’‘ Explanation: The indexOf() method finds the position of an item in an array.


6️⃣ includes() – Checking If Himawari is a Suspect

Masao said, "Maybe Himawari chewed the toy?"
They checked the suspect list:

console.log(suspects.includes("Himawari")); // Output: true

πŸ’‘ Explanation: The includes() method checks if an array contains a specific value.


7️⃣ slice() – Extracting the Real Suspects

They decided Shiro & Himawari were the real suspects, so they created a new suspect list:

let realSuspects = suspects.slice(0, 2); // ["Shiro", "Himawari"]

πŸ’‘ Explanation: The slice() method extracts a portion of an array without modifying it.


8️⃣ filter() – Finding the Guilty One

Shinchan had an idea!
"Let’s check who had access to my room!"
They filtered out people who couldn't enter his room:

let guilty = realSuspects.filter(name => name !== "Bochan");
console.log(guilty); // ["Shiro", "Himawari"]

πŸ’‘ Explanation: The filter() method creates a new array with elements that match a condition.


9️⃣ map() – Preparing the Rescue Mission

They made a rescue plan and mapped out what each of them should do:

let plan = guilty.map(name => `${name} will be watched closely!`);
console.log(plan);

πŸ’‘ Explanation: The map() method transforms each element of an array into something new.


πŸ”Ÿ find() – Catching the Culprit

They saw Himawari chewing something behind the sofa!

let culprit = guilty.find(name => name === "Himawari");
console.log(`${culprit} stole the toy!`);

πŸ’‘ Explanation: The find() method returns the first matching element from an array.


πŸŽ‰ Conclusion: The Toy is Found!

Himawari was caught red-handed, chewing Shinchan's Action Kamen toy!
Misae scolded both Shinchan & Himawari, while Shinchan made a dramatic exit:
"Mom, justice has been served, but I demand a new toy!" 🀣


πŸ” 10 JavaScript Array Methods Used in the Story

MethodUsage in the Story
push()Adding clues to the list
pop()Removing incorrect clues
unshift()Adding a new clue at the beginning
shift()Removing unnecessary clues
indexOf()Checking if Shiro was a suspect
includes()Checking if Himawari was on the list
slice()Extracting real suspects
filter()Narrowing down the culprit
map()Preparing the rescue plan
find()Identifying the thief

More from this blog