What difference does these 2 codes make?


#1


https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661#


consider the 2 different blocks of codes at the bottom. specifically the 'list' function blocks. why does adding console.log() result in having "undefined" shown in the console output (the first and last names still shows up)? does adding an additional console.log() have any implications even though the output I needed would still show up?


> 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];

> function printperson(person) {
>     console.log(person.firstName + " " + person.lastName);
> }

> function list () {
>     var contactsLength = contacts.length;
>     for (var i = 0; i < contacts.length; i++) {
>         printperson(contacts[i]);
>     }
> };

> list ();

> 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];

> function printperson(person) {
>     console.log(person.firstName + " " + person.lastName);
> }

> function list () {
>     var contactsLength = contacts.length;
>     for (var i = 0; i < contacts.length; i++) {
>         console.log(printperson(contacts[i]));
>     }
> };

> list ();


#2

It returns undefined because basically your function doesn't return anything. Use return instead. In this case you can use:

console.log(printperson(contacts[i]));

But I think you don't need it because function printperson() already has console.log() method, so all you need just call the function:

printperson(contacts[i])

#3

In other words, it is not possible to console.log() print a function into the output?


#4

It's possible but it's not correct way. If function has return keyword you can use console.log() to print result in browser console:

function printperson(person) {
    return person.firstName + " " + person.lastName;
}

function list () {
    var contactsLength = contacts.length;
    for (var i = 0; i < contacts.length; i++) {
        console.log(printperson(contacts[i]));
    }
}

If your function already has console.log() you need just call the function to see result in console:

function printperson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list () {
    var contactsLength = contacts.length;
    for (var i = 0; i < contacts.length; i++) {
        printperson(contacts[i]);
    }
}

#5

This cleared my doubts. Thanks for the great help!


#6

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