Am I supposed to get "undefined" in lesson 25?


#1


I am getting "undefined" after every console.log() I make. Where is it coming from ?


function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

// now we  can easily make all of our rabbits
var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");
console.log(rabbit1.describeMyself());
console.log(rabbit2.describeMyself());
console.log(rabbit3.describeMyself());


#2

The output, undefined is the console response to a function call with no return value. Consider that console.log() does not have a return value. If we log it, we get,

    console.log(console.log());    // undefined

This is what is happening above since you are logging console.log()

console.log(rabbit1.describeMyself());

Since the method is logging, try just calling it on the object:

rabbit1.describeMyself();
rabbit2.describeMyself();

#3

A post was split to a new topic: Everything working the way it was supposed to


#4

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