srvjha

JavaScript Array Lessons from Life😊

05/02/2025

6 min read

JavaScript

chai-code

Life lessons

ChaiCode

In this article, we are going to learn 10 JS methods that we can relate to our life experiences. so lets dive into it….

push() Method

In life, to be successful, you need to push certain values into your ā€˜life’ array (your box), like discipline, consistency, hard work, and gratitude.

Similarly, in JavaScript, the push() method adds elements to the end of an array. Now, don’t get confused when I say 'end'—because everything starts from emptiness. Here, 'end' is actually the beginningšŸ˜…. Once you push the first value, it wouldn’t be fair if another value got ahead of it. To maintain fairness, new values are always added at the end.

let successfullLife = ["discipline","consistency","hardwork","gratitude"]
// here suppose i want to add humbleness to my successfull life then i use push method so i will do
successfullLife.push("humbleness")
console.log(successfullLife);
// output :- ["discipline","consistency","hardwork","gratitude","humbleness"]

pop() Method

Now this method is very interesting In life there is always a time when you have to pop out the things that hold onto habits that don’t serve us (like procrastination, self-doubt, or negativity).

Using pop(), we remove the last bad habit that’s holding us back.

let badHabits = ["procrastination", "self-doubt", "overthinking"];
console.log("Before:", badHabits);
// "Output" : ["procrastination", "self-doubt", "overthinking"];

badHabits.pop();  // Removes "overthinking"
console.log("After:", badHabits);
// "After" : ["procrastination", "self-doubt"];

// Also to get the popped value store it inside the variable
let removedHabit  = badHabits.pop(); 
console.log(removedHabit) // "self-doubt"

"Life is like an array. To grow, sometimes you need to push() good habits and pop() the things that no longer serve you." šŸš€

unshift() Method

To achieve any goal, you need to break it down into smaller steps and prioritize them in order of importance. Similarly, in JavaScript, the unshift method adds an element to the beginning of an array, just like how high-priority tasks should come first in a plan.

// goal is to make a project
// let suppose i prepare the array
let tasks = ["Test the app", "Deploy the app"];
console.log(tasks);
// Output: ["Test the app", "Deploy the app"]
/*
Now, you realize that "Plan the project" should come first, 
as it's the foundation for everything else.
You use unshift to add it at the beginning:
*/
tasks.unshift("Plan the project");
console.log(tasks);
// Output: ["Plan the project", "Test the app", "Deploy the app"]

shift() Method

In Life sometimes, we grow certain beliefs, habits, or people that were once important.
In JavaScript: shift() removes the first element of an array

When we’re young, we might focus too much on seeking validation from others. But as we mature, we realize it's not necessary, so we remove it from our mindset.

let mindset = ["seeking validation", "confidence", "growth"];

mindset.shift();  // Removing "seeking validation"
console.log(mindset);
// ["confidence", "growth"]

flat() Method

Sometimes, life feels complicated and cluttered. In those moments, you just need to calm down and simplify things , just like how JavaScript uses the flat() method to flatten nested arrays, making them easier to manage.

let life = [
  ["Work", "Meetings"], 
  ["Family", ["Spending time", "Helping parents"]], 
  ["Health", ["Exercise", "Healthy eating"]], 
  ["Personal growth", ["Learning new skills"]]
];
let simplifiedLife = life.flat();
console.log(simplifiedLife);
//['Work', 'Meetings', 'Family', Array(2), 'Health', Array(2), 'Personal growth', Array(1)]
/*
here () is empty means it will flat out the nested array what at what depth that 
is now known so it flats for only depth 1 so we can specify that
*/
 simplifiedLife = life.flat(2);
//['Work', 'Meetings', 'Family', 'Spending time', 'Helping parents', 'Health', 'Exercise', 'Healthy eating', 'Personal growth', 'Learning new skills']

includes() method

When you apply for a job and submit your resume, modern ATS (Applicant Tracking System) software scans it to check whether it includes specific keywords related to the job.

Similarly, in JavaScript, the includes() method checks whether an array or string contains a specific value.

let resume = ["HTML","CSS","JAVASCRIPT"];
console.log(resume.includes("JAVASCRIPT");) // true;
console.log(resume.includes("REACT")); // false

splice() Method

To improve us as a being we often need to either remove certain habits or replace them with something that add values to our life ,

In JavaScript there is array method names splice() that changes the contents of an array by removing or replacing existing elements and/or adding new elements in place (Uski Jagah)

let habits = ["Pray","Workout","Study","Eating Junk Foods","Watching Insta Reels"]
// lets see how we can insert new habit
habits.splice(2,0,"Toucing Parents Feet") // (startIndex,deletcount,item1);
console.log(habits)
//['Pray', 'Workout', 'Toucing Parents Feet', 'Study', 'Eating Junk Foods', 'Watching Insta Reels']

// lets remove the habit
habits.splice(5,1)
console.log(habits)
//['Pray', 'Workout', 'Toucing Parents Feet', 'Study', 'Eating Junk Foods']

// lets replace one habit
habits.splice(4,1,"Eating Healthy Food")
console.log(habits)
//['Pray', 'Workout', 'Toucing Parents Feet', 'Study', 'Healthy Food']

reverse() method

Life often moves forward, and we focus on the present and future. But sometimes, we need to look back at where we started—to reflect on our past experiences, lessons, and growth.

This is similar to how reverse() in JavaScript flips the order of elements in an array, making us reverse things from the end to the beginning.

let lifeJourney = ["Struggles", "Learning", "Small Wins", "Success"];
lifeJourney.reverse();
console.log(lifeJourney); 
// Output: ["Success", "Small Wins", "Learning", "Struggles"]

find() Method

Imagine you’re swiping through Tinder 😁, looking for the perfect match. You have a list of potential people, but you're only interested in the first person who has a cute dog (because you love dogs!).

let tinderProfiles = [
  { name: "AngelPriya", hasDog: true },
  { name: "Sonam Gupta", hasDog: false },
  { name: "Ava Adams", hasDog: true }
];

const matchedProfile = tinderProfiles.find((profile)=>profile.hasDog === true)
console.log(matchedProfile);
// "AngelPriya" -- > returned the first matching profile with dog

filter() Method

To be successful, one of the key factors is your circle the group of people you surround yourself with. The people around you have a huge impact on your success. That’s why filtering out the good and bad influences in your circle is so important.

Similarly, in JavaScript, there's an array method called filter() that allows you to filter out elements based on certain conditions, keeping only those that are important or relevant

let socialCircle = [
  { name: "Amit", isPositive: true },
  { name: "Raj", isPositive: false },
  { name: "Priya", isPositive: true },
  { name: "Neha", isPositive: false }
];

let positiveCircle = socialCircle.filter(person => person.isPositive === true);

console.log(positiveCircle);
// Output: [ { name: 'Amit', isPositive: true }, { name: 'Priya', isPositive: true } ]

Conclusion

JavaScript array methods teaches us more than just coding, they reflect life!. Life, like arrays, is about how we handle what’s in front of us. Keep coding and keep growing! šŸ’»šŸ’”.

Hope you understand it ,do give it a like ā¤ and also share with your fellow coders.