Need help with explanation


#1

Can someone please help me better understand what is the parameter "person" playing out in the printPerson's function? Or what does this parameter serve as? Thank you

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

// printPerson added here 
var printPerson = function(a/*"person"*/){
    console.log(a.firstName + " " + a.lastName);
}
printPerson(contacts[0]);
printPerson(contacts[1]);

#2

I do not understand your question. What do you exactly want to ask?

As for the code, it does not make any difference whether you write the code as:

Note: /*bits of text*/ is a comment and effectively ignored by the interpreter.

var printPerson = function(a){
    console.log(a.firstName + " " + a.lastName);
}

or:

var printPerson = function(person){
    console.log(person.firstName + " " + person.lastName);
}

both mean the same thing to the interpreter. Remember that the parameter is just a name given to a variable. My name is Gaurang, but if I had been named by my parents as Unicorn, it wouldn't have made any different. I would still be the same. What I meant to say is that naming your parameter as a, person or ponies does not make any difference. But the name should be descriptive, so the name person is the best option.


#3

That's exactly what i'm trying to understand, but how come when I console.log(bob.firstName + " " + bob.lastName); and also mary with its own console.log() the results were different from :
var printPerson = function(a){
console.log(a.firstName + " " + a.lastName);

} `//And either one true, but what's the difference? thanks :slight_smile:


#4

By doing:

console.log(bob.firstName + " " + bob.lastName);

you are accessing the properties of the object bob, and printing their corresponding values.

By doing:

var printPerson = function(a){
    console.log(a.firstName + " " + a.lastName);
};

You are creating a function which takes an object argument and prints the value of its firstName and lastName properties.

The object argument depends on what value the function was passed when it was called. It can be like: printPerson(bob) - here bob is the passed object argument, or maybe printPerson(mary) - where mary is passed.

Remember that when you create a function, you need to also call it with valid arguments to see its result.