0

hello friends I am a little bit confused in js callback function and arguments of it.

Can someone to clarify this code to me(only about callback function and arguments)?

Thank you in advance for your help.

 var allUserData = [];

 function logStuff(userData) { 
  if (typeof userData === 'string') { 
       console.log(userData); 
  } else if (typeof userData === "object") { 
      for (var item in userData) { 
            console.log(item + ": " + userData[item]); 
      }
  }
}
function getInput(options, callback) { 
  allUserData.push(options);
  callback(options); //What is that?
}
getInput({ name: 'Rich', speciality: 'JavaScript' }, logStuff); //How that's work?
jsDev
  • 136
  • 2
  • 12
  • It is a reference to the function that you pass in and it gets called when it is ready. – epascarello Jul 27 '17 at 14:13
  • 2
    `callback(options)` invokes the function in the variable called `callback` which is passed as an argument containing the function `logStuff`, so `logStuff` is called and receives `options` as its argument. – Alex K. Jul 27 '17 at 14:13

0 Answers0