I am learning call back functions in Javascript and hence trying to execute a function that uses call back function
var allUserDdata = [];
function logStuf(userData) {
if (typeof(userData) === "string") {
console.log(userData);
} else if (typeof(userData) === "object") {
for (var items in userData) {
console.log(items, userData[items]);
}
}
}
function getInput(options, callback){
allUserDdata.push(options);
callback(allUserDdata);
}
getInput("Alex", logStuf);
getInput({ "Name": "Alex", "Place": "Malaysia" }, logStuf);
Instead of printing the output as:
0 Alex
1 {Name: "Alex", Place: "Malaysia"}
Its printing the output as:
0 Alex
0 Alex
1 {Name: "Alex", Place: "Malaysia"}
May I know why? Also, I am in the beginner's phase of learning Javascript so please ignore if its a stupid question.