According to below given code the output i expect is=> steve and bill. But the last object's data that is information about Bill also prints out automatically. how to stop it.? and why is that.?


#1

var friends = new Object();
friends.steve={
name: 'Steve',
company: 'Apple inc',
}
friends.bill={
name: 'Bill',
company: 'Microsoft',
}

var list = function(groupName){
for(var names in groupName){
console.log(names);
}
};


#2

tl;dr: It's the console's problem. You won't experience it in normal web dev. Your code is fine.

Long answer:
The console shows the return value of the last statement, if you skip calling the list function (as you have currently done), then the last statement's value evaluates to bill object's information.

if you do call the list function, as here, then the last statement is list() and so the return value would be the value returned value by list, which is of course undefined, so nothing is seen in the console, except the value logged by console.log.


#3

thanks @gaurangtandon :relaxed: