Please, explain the difference between the two call functions:


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/6?curriculum_id=506324b3a7dffd00020bf661#


At the bottom of my code are two functions, the top one was able to call upon the object within the object. The second one was not. As best as I can tell, they are essentially doing the same thing.

Can someone explain to me the difference between the two functions?


//This is object I made for call functions to call upon
var friends= {
    bill: {
    firstName: 'bill',
    lastName: 'nye',
    number: 4086666666,
    address: ['555 coconut land', 'candyland', 'FT', '99999']
},
    steve: {
    firstName: 'steve',
    lastName: 'jobs',
    number: 5105555566,
    address: ['666 devils expressway', '7th level', 'HL', '66666']
}
};

//Call function 1
var list= function(parameter){
    for (var key in parameter){
        console.log(parameter[key].firstName);  //returns 'bill', 'steve'
    }
}

//Call function 2
var list2= function(parameter){
    for (var key in parameter){
        console.log(parameter[key][firstName]);  //returns error 'Did you write a function named list?'
    }
}


#2

key is a variable, firstName is a key name (a string).

parameters[key]['firstName']

#3

here:

console.log(parameter[key][firstName]);

if you want to use square brackets (also known as associative array notation), firstName needs to be a string. If you just want to use property name, you need to use the dot notation like you did in the first function


#4

awesome! Thanks for the quick responses! I see what I did wrong now. Thanks again.


#5

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