" The Mysterious Incident that Happened With Us " πŸ¦‡

" The Mysterious Incident that Happened With Us " πŸ¦‡

Learn JavaScript Array Methods in Story Format

Β·

5 min read

HELLO DEVELOPERS! Today I’ll Share you a story of my life!

A story that has remained unanswered until today. While reading it, we’ll also learn about different Array Methods used in JavaScript and their usecases. So tie-up your seatbelts and let’s just dive right into it.

It was the end of March, our exams had just finished. As our summer vacation had just begun we were heading towards our ancestral village. Our ancestral village life was nothing like that of the city life- there was no electricity. Fields, trees and ponds were spread accross the entire village. Apart from all of these, there was something very mysterious about this place.

Me with my cousin brothers used to roam around every nook and corner of the village. One day as we were planning about where to go, one of my brother's friend suggested that we should visit the scary, lonely, old, broken house which was situated at the end of the village.

Henceforth, as planned five of us gathered (.concat) together at the nearby riverside, and started our adventure. Evening was falling and daylight was gradually fading. As we were walking towards the old, broken house, we heard someone's voice, "Hey! where are you guys going?" I said, today we are going inside the scary house to bust all the myths and ghost stories around it. he replied, "Here, let me also join you guys. My house is just beside the old house so I can guide you through". So he joined (.push) with us and we became a team of six. My cousin brother who lives there exclaimed, "What house? There is no house nearby!" The boy just gave a subtle smile.


πŸ‘» CONCAT METHOD β†’ In JavaScript, .concat() is used to combine multiple arrays or values into one.

Example :

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let result = arr1.concat(arr2);
console.log(result);  // Output: [1, 2, 3, 4, 5, 6]

πŸ‘» PUSH METHOD β†’ In JavaScript, .push() is used to append elements to the end of an array.

Example:

let arr = [1, 2, 3];
arr.push(4);  // Adds 4 to the end of the array
console.log(arr);  // Output: [1, 2, 3, 4]

When we reached the place, to our utter surprise there was actually a new house added at the front row (.unshift) of the house. We were very surprised. "I was here a week ago but there was no house here!" said my brother. But there were two houses in total(.reduce) now.


πŸ‘» UNSHIFT METHOD β†’ In JavaScript, .unshift() adds elements to the start of an array.

Example:

javascriptCopylet arr = [2, 3, 4];
arr.unshift(1);  // Adds 1 to the beginning of the array
console.log(arr);  // Output: [1, 2, 3, 4]

arr.unshift(-2, -1, 0);  // Adds multiple elements at the start
console.log(arr);  // Output: [-2, -1, 0, 1, 2, 3, 4]

πŸ‘» REDUCE METHOD β†’ In JavaScript, .reduce() is used to reduce an array to a single value by executing a callback function on each element, from left to right, accumulating the result.

Example:

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

let sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

console.log(sum);  // Output: 15

We then entered the house with a lamp in hand. The walls were falling apart. One side of the wall was standing in half (.splice). We moved inside a room, it was conquered by the spiders and weeds. All of a sudden my eyes fell upon a book over the bedside table. It was nothing but an old, torned out photo album. After lifting some pages, I noticed a photo of a young boy just about our age. The photo was washed out, but his face was still discernible. I wailed at my brother, "Come Brother! See this!" and we all were extreamly surprised. The boy looked just like that boy who had joined us a moment ago. We looked around all over the place to find (.find) the boy but he was not with us (.pop) anymore. We became a team of five once again.


πŸ‘» SPLICE METHOD β†’ In JavaScript, .splice() method is a powerful array method used to add, remove, or replace elements in an array at any given index.

Example:

let arr = [10, 20, 30, 40, 50];
arr.splice(2, 1);  // Starts at index 2 and removes 1 element
console.log(arr);  // Output: [10, 20, 40, 50]

πŸ‘» FIND METHOD β†’ In JavaScript, .find() method is commonly used to search for an element in an array based on a condition.

Example:

let numbers = [3, 7, 11, 15, 8];

let foundNumber = numbers.find((num) => num > 10);

console.log(foundNumber);  // Output: 11

πŸ‘» POP METHOD β†’ In JavaScript, .pop() method is used to remove the last element from an array.

Example:

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

let removedElement = numbers.pop();

console.log(removedElement);  // Output: 5
console.log(numbers);         // Output: [1, 2, 3, 4]

Horrified we came outside of the house and moved towards his house to find him there. But there was another surprise was waiting for us. There was no new house at that place. It just vanished from the front row (.shift).We were at out wit's end. WHAT DID JUST HAPPEN? We tried to remember what was the placement of the house (.indexof) ? Was it the 1st or the 2nd house? But that was the only house standing there. We all rushed towards home at once.


πŸ‘» SHIFT METHOD β†’ In JavaScript, the .shift() method removes the first element from an array and returns that removed element.

let arr = [1, 2, 3, 4];
let firstElement = arr.shift();

console.log(firstElement); // 1
console.log(arr); // [2, 3, 4]

πŸ‘» INDEXOF METHOD β†’ In JavaScript, the .indexOf() method is used to find the first index of a specified element in an array.

Example:

let arr = [10, 20, 30, 40, 50];
let index = arr.indexOf(30);

console.log(index); // 2

The very next morning, we all went to the place to find if there was only one house (.some). Surprisingly, the old house was the only house standing over there. Until today I could not percieve the reason behind this mysterious incident. It has remained unanswered to us all five.


πŸ‘» SOME METHOD β†’ In JavaScript, the .some() method is used to check if at least one element in an array satisfies a given condition.

Example:

let arr = [1, 2, 3, 4, 5];
let result = arr.some(num => num > 3);

console.log(result); // true
Β