DEV Community

Zokhidjon
Zokhidjon

Posted on

What is the difference between 2 callbacks?

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)
Enter fullscreen mode Exit fullscreen mode

//second code

function printVariable(variable){
    console.log(variable)
}

function second(name, callback){
    callback( `Hello ${name}`)
}

second('Zohidjon', printVariable)
Enter fullscreen mode Exit fullscreen mode

Their outputs are the same.
Can someone explain me what is going on here?

Top comments (2)

Collapse
 
kvetoslavnovak profile image
kvetoslavnovak • Edited

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.

Collapse
 
seven_77 profile image
Seven

They are completely the same.