Array Methods in JavaScript .

Table of contents
- ๐ก Introduction โ Arrays are Like Kitchen Dabbas
- Array Methods :
- ๐ .forEach() โ Checking All Dabbas One by One
- ๐ช .map() โ Adding Labels to Dabbas
- โ .filter() โ Removing Empty Dabbas
- ๐ .find() โ Locate a Specific Dabba
- ๐ .findIndex() โ Finding the Dabbaโs Position
- ๐ .reduce() โ Combining All Quantities
- ๐ .some() & .every() โ Quick Checks
- ๐ .slice() โ Take a Few Dabbas Out
- ๐ฅ .splice() โ Replace Old Masala with New
- ๐งผ .flat() โ Merge All Small Dabbas into One
- Summary :
๐ก Introduction โ Arrays are Like Kitchen Dabbas
Imagine your momโs kitchen โ thereโs a steel rack of dabbas (containers), each holding something: dal, rice, atta, sugar. Thatโs your JavaScript array โ a container that holds multiple values.
Each array method is like a different task you do in the kitchen โ picking out what you need, preparing, rearranging, or discarding.
Letโs explore them with relatable Indian household scenarios:
Array Methods :
๐ .forEach()
โ Checking All Dabbas One by One
Analogy: Mummy is doing a stock check. She opens each dabba, peeks in, and notes if it needs a refill.
const kitchen = ["rice", "dal", "atta", "sugar"];
kitchen.forEach(item => {
console.log(`Checked ${item} dabba`);
});
๐น Purpose: Executes a function for each item.
๐น Use Case: Performing an action without changing the array.
๐น Returns: Nothing.
๐ช .map()
โ Adding Labels to Dabbas
Analogy: You decide to make cute labels for all the dabbas: โRice (5kg)โ, โDal (2kg)โ.
const dabbas = ["rice", "dal", "atta"];
const labeled = dabbas.map(item => `${item} (labelled)`);
console.log(labeled); // ["rice (labelled)", "dal (labelled)", "atta (labelled)"]
๐น Purpose: Create a new array by modifying each item.
๐น Use Case: Changing the format of data.
๐น Returns: A new array.
โ .filter()
โ Removing Empty Dabbas
Analogy: Some dabbas are empty. Mummy removes those from the main shelf.
const shelf = ["rice", "", "dal", "", "atta"];
const nonEmpty = shelf.filter(item => item !== "");
console.log(nonEmpty); // ["rice", "dal", "atta"]
๐น Purpose: Keep only items that match a condition.
๐น Use Case: Removing unwanted or invalid items.
๐น Returns: A new array with selected elements.
๐ .find()
โ Locate a Specific Dabba
Analogy: Youโre hunting for the sugar dabba in the stack.
javascriptCopyEditconst kitchen = ["rice", "dal", "sugar", "atta"];
const found = kitchen.find(item => item === "sugar");
console.log(found); // "sugar"
๐น Purpose: Return the first element that matches a condition.
๐น Use Case: Searching for a specific value.
๐น Returns: The item (not an array).
๐ .findIndex()
โ Finding the Dabbaโs Position
Analogy: You found the tea dabba, but want to know where it is placed.
const items = ["salt", "tea", "coffee"];
const index = items.findIndex(item => item === "tea");
console.log(index); // 1
๐น Purpose: Find the index of the first matching item.
๐น Use Case: When position matters.
๐น Returns: Index number.
๐ .reduce()
โ Combining All Quantities
Analogy: Youโre totaling the weight of all dabbas to know how much ration is left.
const weights = [5, 2, 3]; // rice, dal, atta in kg
const total = weights.reduce((sum, kg) => sum + kg, 0);
console.log(total); // 10
๐น Purpose: Combine all values into one (sum, product, etc)โฆhere second argument is the initial value of the sum i.e 0โฆand then each weight is added to it.
๐น Use Case: Calculations on an array.
๐น Returns: A single value.
๐ .some()
& .every()
โ Quick Checks
Analogy:
.some()
โ โIs any dabba empty?โ.every()
โ โAre all dabbas full?โ
const stock = [5, 3, 0, 2];
console.log(stock.some(qty => qty === 0)); // true
console.log(stock.every(qty => qty > 0)); // false
๐น Use Case: Validation and early checks.
๐น Returns: Boolean.
๐ .slice()
โ Take a Few Dabbas Out
Analogy: You remove the first two dabbas to refill them, leaving the rest untouched.
const rack = ["rice", "dal", "atta", "sugar"];
const toRefill = rack.slice(0, 2);
console.log(toRefill); // ["rice", "dal"]
๐น Use Case: Extract part of an array without changing the originalโฆextracts the items exclusively at ending index.
๐น Returns: A shallow copy of a portion.
๐ฅ .splice()
โ Replace Old Masala with New
Analogy: You remove spoiled masala and put in fresh ones at the same place.
const shelf = ["haldi", "mirchi", "garam masala"];
shelf.splice(1, 1, "jeera"); // remove 1 item at index 1 and insert "jeera"
console.log(shelf); // ["haldi", "jeera", "garam masala"]
๐น Use Case: Modify array by removing/replacing items.
๐น Returns: Removed elements.
๐งผ .flat()
โ Merge All Small Dabbas into One
Analogy: Mummy has smaller boxes inside a big box โ now you empty them all into a single big dabba.
javascriptCopyEditconst nested = [["dal", "chana"], ["rice", "sugar"]];
const merged = nested.flat();
console.log(merged); // ["dal", "chana", "rice", "sugar"]
๐น Use Case: Flatten nested arrays.
๐น Returns: A new flattened array.
Summary :
Method | Description | Mutates Original Array | Return Type |
forEach() | Executes a callback for each element in the array | โ No | undefined |
map() | Creates a new array by transforming each element using a callback | โ No | Array |
filter() | Creates a new array with elements that pass a test implemented by callback | โ No | Array |
find() | Returns the first element that matches the condition | โ No | Element or undefined |
findIndex() | Returns the index of the first matching element | โ No | Number (-1 if not found) |
reduce() | Reduces the array to a single value using an accumulator function | โ No | Depends (any type) |
some() | Returns true if any element passes the test | โ No | Boolean |
every() | Returns true if all elements pass the test | โ No | Boolean |
slice() | Extracts a section of the array into a new array | โ No | Array |
splice() | Adds, removes, or replaces elements in-place | โ Yes | Array (removed elements) |
flat() | Flattens nested arrays into a single-level array | โ No | Array |
Conclusion
JavaScript array methods offer powerful, declarative ways to manipulate and process data efficiently. By understanding their individual behaviors and return types, developers can write cleaner, more readable, and maintainable code.
Understanding when and how to use these methods leads to more expressive and performant code.
For a deeper dive into these methods, detailed syntax, browser support, and edge cases, it's highly recommended to explore the official MDN Web Docs โ Array.
Subscribe to my newsletter
Read articles from Ansh Kotnala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
