7. Why does console.log(friends.key) print `undefined` inside the search function?


#1

I don't understand why console.log(friends[key]) works inside the function to print to the console, but console.log(friends.key) does not. The second command console.log(friends.key) does work when it's not written inside the function...see below.

var search=function(name) {
    name=name.toLowerCase();
    console.log(" Searching for " + name + "...");
    for (var key in friends) { 
        if (key===name) {
            console.log(friends.key);
            // console.log(friends[key]);
            return friends[key];
        }
    }
};
console.log(friends.steve);

console.log("Output from search('steve')'");
search("steve");
console.log("");

The output is

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(550)555-1958',
  address: [ '17 Apple Drive', 'Portland', 'OR', '00000' ] }
Output from search('steve')'
 Searching for steve...
undefined

So---not inside the function, console.log(friends.key) returns just fine. Inside the function, doesn't work. I tried console.log(friends[key]) (currently commented out), and that command does work inside the function. But why does the other command only work outside the function?


#2

@mimi314159,

var friends= {
              bill: { firstName: 'Bill',
                      lastName: 'Gates',
                      number: '444-444-4444',
                      address: [ 'One Microsoft way', 'Redwood', 'WA' ]
              },
             steve: {
                      firstName: 'Steve',
                      lastName: 'Jobs',
                      number: '333-333-3333',
                      address: [ 'Two infinite loop', 'Bentown', 'DV' ] 
              }
   };

keep repeating these two lines
An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated value

Description of the friends object.
The friends object has 2 properties and are seperated by a comma-,

there is a bill property with property-key bill and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)
there is a steve property with property-key steve and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)

access via dot-notation == only literal property-key allowed

friends.bill ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends.bill.lastName ==> you will get the associated value of the lastName property-key

access via the square-bracket-notation == literal & VARIABLE

1 using the literal property-key

friends["bill"]
friends["bill"]["lastName"]

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "bill";
var propertyKey2 ="lastName";
friends[propertyKey1] ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends[propertyKey1][propertyKey2]
in this case you will get the string VALUE of the lastName property
of the associated object VALUE of bill


#3

Gotcha. That makes perfect sense. Thank you for your explanation.