๐บ๏ธ JavaScript Map & ๐ Higher-Order Functions
Mastering Map & Higher-Order Functions in JavaScript ๐

I am a passionate MERN Developer with comprehensive expertise in MongoDB, Express.js, React, and Node.js, coupled with proficiency in PostgreSQL. Experienced in full-stack development, I excel in crafting dynamic and scalable applications tailored to meet diverse business needs. With a keen eye for detail and a commitment to continuous learning, I thrive on delivering high-quality solutions that drive user engagement and business growth. Let's collaborate and leverage the power of technology to bring your project ideas to fruition!"
JavaScript gives us powerful tools to handle data structures and perform array transformations in a clean way.
Today, letโs explore:
โ
Map Object
โ
Higher-Order Functions (map, filter)
๐บ๏ธ 1. The Map Object
๐ The Map object stores keyโvalue pairs, just like an object, BUT with a twist:
Keys can be of any data type (string, number, object, etc.)
Maintains the insertion order
โจ Creating a Map
let mymap = new Map();
// Add data (key โ value)
mymap.set("name", "Suraj");
mymap.set("age", 22);
mymap.set("22", "Hardy"); // key is string
mymap.set(22, "Sumit"); // key is number
๐ Accessing Data
console.log(mymap.get("22")); // Hardy
console.log(mymap.get(22)); // Sumit
console.log(mymap.get("name")); // Suraj
โก Notice: "22" (string) and 22 (number) are different keys.
โ Deleting Data
mymap.delete(22);
mymap.delete("22");
mymap.delete("name");
console.log(mymap);
๐ Iterating Over Map
for (let [key, value] of mymap) {
console.log(key, value);
}
๐ 2. Higher-Order Functions
๐ A higher-order function is a function that:
1๏ธโฃ Takes another function as input, OR
2๏ธโฃ Returns a new function
function newfunction() {
console.log("I am new function");
}
function highOrderFunction(funct) {
funct(); // call the function
}
highOrderFunction(newfunction);
// Output: I am new function
๐ 3. Useful Higher-Order Functions
๐น map() โ Transform values in an array
const arr = [5, 45, 2, 6];
// Double each element
const result = arr.map((x) => 2 * x);
console.log(result);
// output
//[10, 90, 4, 12]
๐น filter() โ Extract values that match a condition
const arr = [5, 45, 2, 6];
// Keep only values > 2
const result = arr.filter((x) => x > 2);
console.log(result);
// [5, 45, 6]
:
๐ Callback Function โ A function passed into another function to be run after the first one finishes.
function greet(name , callback)
{
console.log("Hello " + name);
callback();
}
function greetings()
{
console.log("good morning");
}
greet("Suraj" ,greetings )
// output
//Hello suraj
//good morning

(you can copy the code and check the output on given links :โ https://www.programiz.com/javascript/online-compiler/
)
๐ฏ Wrap-Up
๐บ๏ธ Map object โ stores keyโvalue pairs, supports any data type as key
๐ Higher-order functions โ take/return functions
๐ map() โ transforms arrays
๐ filter() โ extracts based on condition
Callback()โ call function inside function



