7/8 Search names - Where did name in "var search = function(name)" came from?


#1

I understand 99% of a code, but I have no idea where is program find a name.
There is only firstName and lastName.
No var called name is in the code before creating a function.

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: 234567,
address: ['one', 'street', '2345']

},
steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number: 2134,
    address: ['asd','saf']
}

};

var list = function(friends)
{
for(var key in friends)
{
console.log(key);
}
};

var search = function(name)
{
for(var key in friends)
{
if(name === friends[key].firstName)
{
console.log(friends[key]);
return friends[key];
}
}
}


#2

Name is just a convention variable name for a function. It just as easily could've been x or n, it's only to help you remember what data your function uses.


#3

So when the script workes, where is it finding input for the search function? Could I leave it empty?


#4

It is where you call it, for example you call:

search(steve);
search(bill);

No, you cannot leave it blank.


#5

I still don't uderstand, in all the functions from previous lessons I had to put there some var that exists and has some value. In here I do not see any value inside this name var. Is it an empty var, or is scrip connecting it to the objects?


#6

It is nothing, it just lets you know that your function needs information to run.


#7

So let's say it is a part of syntax that has to be there, but is not doing anything in this script?


#8

I think you should read this:

MDN - Functions


#9

Name is the argument that the function is taking/expecting. It does not have a value until you pass in an argument when calling the search function, for example:

yourFunction("Hello!")

would assign "Hello!" to greeting in:

var yourFunction = function (greeting) {consol.log(greeting)}; // would print "Hello!"