Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays – (Code in Conversation)πŸ“²

Published
β€’7 min readβ€’View as Markdown
JavaScript Arrays – (Code in Conversation)πŸ“²

Let's dive into 10 essential array methods using our WhatsApp family drama! πŸŽ‰

πŸ“Œ What is an Array?

An array is just like thatβ€”a special list where you can store multiple values together. Instead of making separate variables for each family member, you store them all in one array.


πŸ’‘ Example: Creating an Array

Without Arrays (Very Annoying)

let member1 = "Mummy";
let member2 = "Papa";
let member3 = "Dadi";
let member4 = "Chacha";
let member5 = "Cousin";

😩 Too much typing! Who has time for this?

With an Array (So Much Better!)

let familyGroup = ["Mummy", "Papa", "Dadi", "Chacha", "Cousin"];
console.log(familyGroup);

//Output: ["Mummy", "Papa", "Dadi", "Chacha", "Cousin"]

πŸ₯± You wake up, open WhatsApp, and… BAM! 257 unread messages.

Your phone is struggling to breathe under the weight of "Good Morning 🌞🌹" messages and blurry pictures of random gods. Welcome to the Indian Family WhatsApp Group, aka the perfect way to explain JavaScript array methods! πŸŽ‰

Let's dive deep into arrays and it’s 10 Method using this Indian WhatsApp family drama! πŸŽ­πŸ“²


0️⃣ Declaring the Array (Creating the WhatsApp Group)

πŸ“² "You have been added to 'Happy Family ❀️'"

let familyGroup = ["Mummy", "Papa", "Me"];
console.log(familyGroup);

//Output: ["Mummy", "Papa", "Me"]

πŸ’‘ Explanation:

  • A JavaScript array is a list that stores multiple values in a single variable.

  • Here, we created an array called familyGroup, initially containing only "Mummy" and "Papa".

πŸ‘¦ You (thinking): "Chalo, only mummy and papa. Manageable."

BUT WAIT…


1️⃣ push() – Adding More Family Members at the End

πŸ‘©β€πŸ‘¦ Mummy: β€œBeta, I’ve added some more people.”

πŸ“² "Dadi, Dadaji, Bua, Chacha, 7 cousins have been added to 'Happy Family ❀️'"

familyGroup.push("Dadi", "Dadaji", "Bua", "Chacha", "Cousin 1", "Cousin 2", "Cousin 3", "Cousin 4", "Cousin 5", "Cousin 6", "Cousin 7");
console.log(familyGroup);

//Output: ["Mummy", "Papa", "Me", "Dadi", "Dadaji", "Bua", "Chacha", "Cousin 1", "Cousin 2", "Cousin 3", "Cousin 4", "Cousin 5", "Cousin 6", "Cousin 7"]

πŸ’‘ Explanation:

  • .push() adds elements to the END of the array.

  • Now, our familyGroup has more chaos added at the end.


2️⃣ pop() – Removing the Last Person from the Group

You pray that Chacha, who keeps forwarding "Share this to 10 people or bad luck will follow" messages, finally leaves the group. But instead…

πŸ“² "Cousin 7 has left the group."

let removedPerson = familyGroup.pop();
console.log(`${removedPerson} has left the group`);
console.log(familyGroup);
Output: //Cousin 7 has left the group

["Mummy", "Papa", "Me", "Dadi", "Dadaji", "Bua", "Chacha", "Cousin 1", "Cousin 2", "Cousin 3", "Cousin 4", "Cousin 5", "Cousin 6"]

πŸ’‘ Explanation:

  • .pop() removes the LAST element from the array.

  • Since our last person in familyGroup was Cousin 7, they got removed instead of Chacha. *Life is unfair!*😭

    πŸ‘©β€πŸ‘¦ Mummy: β€œArrey beta, Cousin 7 kyu chala gaya?!”
    πŸ§‘ Cousin 7: "Arre, maine toh bas group mute karna tha!"
    πŸ“² "Mummy added Cousin 7 to 'Happy Family ❀️'."


3️⃣ unshift() – Adding a New Member at the Beginning

πŸ‘©β€πŸ‘¦ Mummy: "Beta, Sharma ji ke bete ko bhi add kar diya."

familyGroup.unshift("Sharma Ji Ka Beta");
console.log(familyGroup);

// Output: ["Sharma Ji Ka Beta", "Mummy", "Papa", "Me", "Dadi", "Dadaji", "Bua", "Chacha", "Cousin 1", "Cousin 2", "Cousin 3", "Cousin 4", "Cousin 5", "Cousin 6", "Cousin 7"]

πŸ’‘ Explanation:

  • .unshift() adds elements at the BEGINNING of the array.

  • Now "Sharma Ji Ka Beta" is at the first position in our familyGroup.


4️⃣ shift() – Removing the First Person from the Group

πŸ‘¦ You: "Par woh toh family nahi hai?"
πŸ‘©β€πŸ‘¦ Mummy: "Arrey woh toh kab ka family jaisa hai!"

let removedFirstPerson = familyGroup.shift();
console.log(`${removedFirstPerson} has left the group`);
console.log(familyGroup);
// Output: Sharma Ji Ka Beta has left the group
["Mummy", "Papa", "Me", "Dadi", "Dadaji", "Bua", "Chacha", "Cousin 1", "Cousin 2", "Cousin 3", "Cousin 4", "Cousin 5", "Cousin 6", "Cousin 7"]

πŸ’‘ Explanation:

  • .shift() removes the FIRST element from the array.

  • "Sharma Ji Ka Beta" is removed (finally!).

πŸ‘©β€πŸ‘¦ Mummy: "Beta, sharam nahi aati? Sharma Ji ke saamne kaise muh dikhaoge?"


5️⃣ indexOf() – Finding Who Sent That Cringe Good Morning Image

πŸ“² Cousin 3 sent a β€˜Good Morning πŸŒžβ€™ image with 200 flowers and β€˜Jai Shree Krishna’ written in Comic Sans.

πŸ‘¦ You: "WHO DID THIS?!"

let culpritIndex = familyGroup.indexOf("Cousin 3");
console.log(`Cousin 3 is at index: ${culpritIndex}`);
//Output: Cousin 3 is at index: 9

πŸ’‘ Explanation:

  • .indexOf() finds the position of an element in the array.

  • "Cousin 3" is at position 9 in our group.


6️⃣ slice() – Viewing Only Important Messages

πŸ‘¦ You: "Mujhe sirf mummy aur papa messages padhne hain, bas!"

let importantMembers = familyGroup.slice(0, 3);
console.log("Important Members: ", importantMembers);
//Output: Important Members: ["Mummy", "Papa", "Me"]

πŸ’‘ Explanation:

  • .slice(start, end) extracts a portion of the array without modifying the original.

  • This returns only Mummy, Papa, and Me.

πŸ“² Filtered group: ["Mummy", "Papa", "Me"]


7️⃣ filter() – Removing the Forward Gang

πŸ“² Bua, Chacha, and Cousin 3 keep forwarding fake news.

πŸ‘¦ You: "Yeh sab hatao yaar!"

let nonSpammers = familyGroup.filter(member => !["Bua", "Chacha", "Cousin 3"].includes(member));
console.log("Spam-Free Group:", nonSpammers);
//Output: Spam-Free Group: ["Mummy", "Papa", "Me", "Dadi", "Dadaji", "Cousin 1", "Cousin 2", "Cousin 4", "Cousin 5", "Cousin 6", "Cousin 7"]

πŸ’‘ Explanation:

  • .filter() creates a new array with only the elements that match the condition.

  • We removed the people responsible for ruining your mornings.


8️⃣ map() – Everyone Wishes You Happy Birthday in Caps Lock

πŸŽ‚ It’s your birthday. Suddenly:

let birthdayWishes = familyGroup.map(member => `${member}: HAPPY BIRTHDAY!!!`);
console.log(birthdayWishes.join("\\n"));

/* Output: 
Mummy: HAPPY BIRTHDAY!!!
Papa: HAPPY BIRTHDAY!!!
Me: HAPPY BIRTHDAY!!!
Dadi: HAPPY BIRTHDAY!!!
... 
*/

πŸ’‘ Explanation:

  • .map() modifies every element in the array and returns a new array.

  • Now, everyone is shouting "HAPPY BIRTHDAY" at you.

πŸ‘¦ You: "Thankyou Thankyou!!"


9️⃣ forEach() – The Daily β€˜Good Morning’ Spam

familyGroup.forEach(member => console.log(`${member}: Good Morning πŸŒžπŸŒΊπŸ™`));
/*Output: 
Mummy: Good Morning πŸŒžπŸŒΊπŸ™
Papa: Good Morning πŸŒžπŸŒΊπŸ™
Me: Good Morning πŸŒžπŸŒΊπŸ™
...
*/

πŸ’‘ Explanation:

  • .forEach() loops through the array and executes a function for each element.

  • Now, EVERYONE sends Good Morning messages EVERY DAY.

πŸ˜΅β€πŸ’« "IT. NEVER. ENDS."


πŸ”Ÿ includes() – Checking If Mummy Is Still in the Group

πŸ‘¦ You: "Mummy toh hai na group me?"

let isMummyHere = familyGroup.includes("Mummy");
console.log(`Is Mummy still in the group? ${isMummyHere}`);

πŸ’‘ Explanation:

.includes() checks if a particular element exists in the array and returns true or false.

βœ… If "Mummy" is in familyGroup, it prints:

Mummy still in the group? true

🚫 If she got annoyed and left the group, it prints:

Mummy still in the group? false

πŸ‘¦ You: "Nahi! MUMMY KO WAPAS ADD KARO!!"
πŸ‘©β€πŸ‘¦ Mummy: "Beta, maine khud leave kiya!"
πŸ“² "Papa added Mummy to 'Happy Family ❀️'."

πŸ˜‚ Lesson: No one leaves an Indian family WhatsApp group for long!

πŸ“œ Summary Table

MethodAction Performed
push()Adds elements to the end of the array.
pop()Removes the last element from the array.
unshift()Adds elements to the beginning of the array.
shift()Removes the first element from the array.
indexOf()Finds the index of a specific element in the array.
slice()Extracts a portion of the array without modifying the original.
filter()Creates a new array with elements that pass a specific condition.
map()Modifies each element of the array and returns a new array.
forEach()Loops through the array and executes a function for each element.
includes()Checks if an element exists in the array, returning true or false.

Conclusion:

And that's the wrap on JavaScript arrays! Arrays are your powerful tool for storing and managing multiple values in a single variable. Whether you're adding new items, removing old ones, or rearranging them, arrays make handling data a breeze. Keep practicing, and soon you'll be managing data like a pro with the help of these handy array methods.

Happy coding! πŸ’»πŸš€


πŸ”— References & Further Reading

For a deeper dive into the topics covered in this article, check out the following resources:

A huge thanks to the following resources and mentors who helped shape this article:

πŸ”Ή Special Thanks To:

πŸ”Ή Informative Articles & Guides:

  1. Chai Code

  2. MDN – Array - JavaScript | MDN

πŸ”Ή Tools Used:

  1. Figma – Used for designing diagrams and illustrations.

  2. Chatgpt – Used for document structure and content improvement.

S
Satyarth1y ago

funny yet informative... good one!

1