Sitemap

Callback Function with parameters in JS

1 min readJun 3, 2025

--

function greetUser(name, callback) {
//console.log("Hi " + name);
const num = '66'
const num2 = '67'

callback(num);
}



greetUser("Alice", (param, param2) => {
console.log(param);
});

here the confusion ‘how the param or param2 in callback func getting the value of num. it is actually JS IFFE concept.

function greetUser(name, callback) {
//console.log("Hi " + name);
const num = '66'
const num2 = '67'

callback(num); // here callback is becoming this
↓
(param, param2) => { console.log(param); } (num);

// and here (num) is actually agrumnent and param/param2 is parameteres. num
// is passed to param.

}

now lets see this is express

app.get('/', (req, res) => {
res.send('Hello World!');
});

so here req and res are actually parameter, these were given to use req,res’s methods like .send .

Get Crashtuber’s stories in your inbox

Join Medium for free to get updates from this writer.

const res/req = //something;

so whole res/req thing can be used. i think error works in same way in try/catch.

--

--