Why doesnt return work in this situation? but console.log does work?


#1

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(person)
{
return person.firstName +" "+ person.lastName ;
};
printPerson(contacts[0]);
printPerson(contacts[1]);


#2

because the exercise wants you to use return

return (function handing you something back) and console.log (logging something to the console) are very different things


#3

thats the thing i dont understand, i have been having the impression that you can use return in a function and i expect it will work the same way as console.log....or u can use return to stop a loop in a while loop...i am very confused about this return thing...i need some examples to help me understand the diff


#4

simplest example i can think of:

var example = function(){
    return "Hello world"
}
x = example()
console.log(x)

the function example returns a string. so then when you call the function, i store whatever the function returned in a variable (x), which i then log to the console

so the function literal hand me something back (on which, if i want, i could do further manipulation)

console.log() only logs to the console


#5

okay got it thank you very much :smile:


#6

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