7. Search for a friend - friends[prop]



I was able to pass the exercise, however I have a question regarding the search function.
When we return friends[prop], why does the interpreter display the properties of bill and steve (firstName, lastName, number, address).

Aren't the only properties of friends "bill and steve"?

var friends = {};
friends.bill = {
    firstName: 'Bill',
    lastName: 'Madison',
    number: "123-456-7890",
    address: ['9182 Williams Rd', 'San Antonio', 'TX', '78291']
friends.steve = {
    firstName: 'Steve',
    lastName: 'Hangy',
    number: "908-745-4123",
    address: ['4810 Langley Drive', 'Alto Palo', 'CA', '91820']

var list = function(friends) {
    for (var prop in friends) {

var name = ['Bill', 'Steve'];
var search = function(name) {
    for (var prop in friends) {
        if (friends[prop].firstName === name) {
        return friends[prop]



yes, bill and steve are the properties/keys of friends. So prop contains bill and steve. But by doing friends[prop] you use prop as key to get the values

friends has properties/keys and values.


Hey again Stetim

Okay, this makes sense. By doing return friends[prop], we can return the values of bill of steve.

This prints out both the props firstName, lastName, number, and address and their values.


of sorts, it actually prints the bill and steve object.

i should explain better.

when you loop here:

for (var prop in friends)

prop contains a string representation of your object name. by doing friends[prop] you get access to your bill (and steve) object. so friends[prop] actually prints your bill and steve object. (so both the properties and values of bill and steve object)



So in the list function, we are returning bill and steve because the for/in loop is looking for the property of friends and logging the property name (as string)

In the search function, we specifically use friends[prop] to avoid printing the string. This allows us to get both the key and property and the values of bill and steve object.



Many people move through this course without understanding the full concepts. Part of the problem is that in JS objects are also associative arrays (article)(mindfuck)

lets say bill is not part of friends, but just an object/associate arrays:

bill = {
    firstName: 'Bill',
    lastName: 'Madison',
    number: "123-456-7890",
    address: ['9182 Williams Rd', 'San Antonio', 'TX', '78291']

we can access values by using properties:


or by using keys:


with for in loops we have to use keys (associate array notation) because the keys (i use keys here on purpose, not properties) are strings.

Does it so far still make sense?


Phew... it's a lot to take in at once.

I get the basic concept. In our first for/in loop the computer is searching the code for keys of friends object and returning them in string format: 'bill' and 'steve'.

This would be fine if we were looking to display the key as string.

However, the goal is to get the friend's contact information. To do this, we need to display keys and their corresponding values. This is known as an associative array. By returning friends[prop], the interpreter will show us both the keys and the values related to friends.


Yes, its a lot. But you seemed to be understanding and learn it quickly :slight_smile:


:slight_smile: Thanks Stetim! It's much easier to understand the concepts with your explanations.


Any questions about what i explained?


Nope, I don't have any more questions. Thanks!


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