The key value explanation


#1

Need the explanation about 'key' value at the last code, why i hve to use it ?

var friends = {
    bill: {
        firstName:"Bill",
        LastName:"Gates",
        Number: "273",
        address:['rajshahi', 'Dhaka']
        } , 
        
        steve:{
            firstName: "Steve",
            lastName:"Jobs",
            number:"8768",
             address:['rajshahi', 'Borisal']
            }
    
    };
    
    var list = function (contacts){
    for (var key in contacts){
        console.log(key);
    }
};
list(friends);

#2

It's just a local scope parameter to be used in the for in loop.

It's equivalent to x if you were to write

for ( var x = 0; x < 10; x++) {
  // Do something
}

The key parameter is representing - in this context - the key (or property) which you are targeting from the object you pass through. In this exercise that object represents a contact list, and the key represents the first tier properties (which happen to be objects themselves) which are people.


#3

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