Table of contents
- Array.push()- Add more sectors
- Array.shift()
- Array.unshift()
- Array.slice()
- Array.indexOf() – Find the Position of a Sector
- Array.forEach() – Go Through Every Sector
- Array.map() – Transform Sector Names
- Array.sort() - Arrange Funds in ascending
- Array.reverse() - Reverse the order of the sectors
- Array.pop() - Pop the Old tax slabs
Let’s try to understand the Union Budget using Array Methods of Javascript.
The Ministry of Finance created a budget for the Annual Financial year 2025-26 in which they made a yearly money plan.
So to create this budget, the ministry needed to know how many sectors are there.
let sectors = ["Education", "Healthcare","Agriculture"];
Array.push()- Add more sectors
Meanwhile, they heard “Hey! I want to play “
So the Js added the boy in green using the push method.
The push()
method in JavaScript adds one or more elements to the end of an array and returns the new length of the array.
Here’s how you can implement it.
let sectors = ["Education", "Healthcare","Agriculture"];
sectors.push("Infrastructure");
console.log(sectors); // Output: ["Education", "Healthcare","Agriculture","Infrastructure"]
Array.shift()
shift()
removes the element at index 0 (first element) and returns it.
let sectors = ["Education", "Healthcare","Agriculture"];
sectors.shift();
console.log(sectors); // Output: ["Healthcare","Agriculture"]
Array.unshift()
unshift()
method adds elements to the zeroth index of the array(at the beginning).
let sectors = ["Education", "Healthcare","Agriculture"];
sectors.shift("Defence");
console.log(sectors);
Array.slice()
slice()
the magic method that returns a portion you need at the instance.
let sectors = ["Education", "Healthcare","Agriculture"];
let sector = sectors.slice(0,2);
console.log(sector); // output: ["Education", "Healthcare"]
Array.indexOf() – Find the Position of a Sector
indexOf() finds the position of the element in the array.
let sectors = ["Education", "Healthcare", "Agriculture"];
console.log(sectors.indexOf("Agriculture")); // 2
Array.forEach() – Go Through Every Sector
forEach() retrieves every array element and applies a function to it.
let sectors = ["Education", "Healthcare", "Agriculture"];
let funds = [50000, 75000, 30000];
sectors.forEach(function(sector, index) { // index: position of each element in the sectors array
console.log(sector + " gets a fund of $" + funds[index]);
});
//Output:
//Education gets a fund of $50000
//Healthcare gets a fund of $75000
//Agriculture gets a fund of $30000
Array.map() – Transform Sector Names
It creates a new array with the results of the function of every element in the array.
let sectors = ["Education", "Healthcare", "Agriculture"];
let funds = [50000, 75000, 30000];
let sectorFunding = sectors.map((sector, index) => ({
// index represents the position of the current element in the original array.
sector: sector,
fund: funds[index]
}));
console.log(sectorFunding);
Array.sort() - Arrange Funds in ascending
The sort() arranges elements of the array. The default sort order is ascending.
let funds = [50000, 75000, 30000];
funds.sort();
console.log(funds); // output: [30000, 50000,75000]
Array.reverse() - Reverse the order of the sectors
It reverses the order of the sectors in the array.
let sectors = ["Education", "Healthcare", "Agriculture"];
sectors.reverse();
console.log(sectors); // ["Agriculture","Healthcare","Agriculture"]
Array.pop() - Pop the Old tax slabs
This method removes the last element of the array.
let taxRegimeYear = [2025,2024];
taxRegime.pop();
console.log()taxRegime; // Output: [2025]
Hope got the idea of Array Methods in Javascript :)