Search function


#1


My code has worked properly but I do not really understand the line if (friends[key].firstName === name ) .My question is why can i put 'name' in the if statement if it is undefined?


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


#2

it is not? It is the function parameter, which serves as placeholder until you pass along a function argument with the function call:

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

now name holds Bill


#3

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