I cannot catch the idea of 2 callbacks in JavaScript:
//first code
function printVariable(variable){
console.log(variable)
}
function second(name, printVariable){
printVariable( `Hello ${name}`)
}
second('Zohidjon', printVariable)
//second code
function printVariable(variable){
console.log(variable)
}
function second(name, callback){
callback( `Hello ${name}`)
}
second('Zohidjon', printVariable)
Their outputs are the same.
Can someone explain me what is going on here?
Top comments (2)
In the first code example the argument of the function “second” is using the same name as is the name of the function printVariable. In the second code example the name “callback” is used for this argument. You can use anything as the argument name. So consider “printVariable” name usage for the function as well as for the argument as a coincidence. Usually it is better to use unique names for variables and for arguments.
They are completely the same.