List 'em all - need help understanding the logic of the list function and for/in loop


My code seems to behave correctly - I just don't understand why!

I know I have not called the function, but when I do call the function with 'list (friends)' at the end of the below block of code, it works.

Maybe I'm confused by the for / in loop? I just don't understand why the output is:

where is the function getting these names from? Sorry if this is a confusing question.

var friends = {};
friends.bill = {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
friends.steve = {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(405) 555-5555",
        address: ['Two Microsoft Way', 'Redmond', 'WA', '98052']
friends.william = {
        firstName: "Will",
        LastName: "Karnadi",
        number: "(917) 650-7028",
        address: ['49-01 28th Avenue', 'Woodside', 'NY', '11377']
friends.chopski = {
        firstName: "Alexander",
        LastName: "Chopski",
        number: "(212) 546-5250",
        address: ['Some street', 'Seattle', 'WA', '98052']

var list = function(something) {
    for (var nonsense in friends) {


var list = function(something) {
    for (var nonsense in friends) {

Your function's parameter is something but you're iterating over friends ?


Thanks. Let's say I replace the function parameter with friends. (Which I would do by writing 'list(fiends)' in the end anyway). How does it work?


Lets understand two important concepts..
updated @illyay2015
global variables --

variables defined outside of a function are called in global scope..and we call them 'global variables' . They can be accessed from any place of the program.

local variable --
Variables defined inside the function,They only exist till function runs.
When we pass an argument to a function,it just replace all variables(inside of function) with values and does whatever process function have to do on data(arguments).
When execution of function halts,The variables inside of function convey no meaning.

lets understand this thing with an example..

var x = 4;
var y = 5;
var z = 10;

// x,y z are in global scope as they defined outside of function.

function double(a){
    return a * 2;
// variable a here just act like a placeholder and it's in local scope.

console.log(double(x)); // It will o/p  8
console.log(double(y)); // will o/p 10
console.log(double(z)); // will o/p  20

Now try one more example..

function Setsvalue(){
    var  m = 4;
// A function that sets a variable m to 4.
//Try to print that..


//and tell me what you get?

The reason why you're still think that there should be friends not something because you think 'I have to use friends Object so i will loop over friends.'

You can create any Object of same format as mentioned in problem and pass to this function,It will work.

remember: Function aren't made to solve one problem but many problems of same nature.

You define local variable(inside the function) and when you have to use function,You call it and you just pass values to that function , local variable (inside the function) will replaced by arguments(values) you pass.

Hope it makes doubt clear?
do reply !:slight_smile:


This is awesome. Thank you, @greentreemee! You have been tremendously helpful. I mean - I thought I kind of had it - but was very unsure; but in the end you helped me understand better.

In your second example, I believe you would get an undefined variable error, as m is only defined in the local scope of the function 'Setsvalue' but not in global scope. Is this correct?

Thanks again for your detailed commentary.


Yes that's correct!!!(Actually It will throw an error)

as Its in local scope!
cheers! :thumbsup:


Cheers to you! Thanks :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.