Why am I getting "Undefined" in the resposne?

Tell us what’s happening:
// Custom code - trying to understand inheritance
I am getting the output as :

Vroooooooooom
Undefined

Why am I getting the “undefined” part in the response?

Your code so far


let Vehicle = function (tires,color,engine){
this.tires = tires;
this.color = color;
this.engine = engine;
}

Vehicle.prototype = {
constructor: Vehicle,
steering: true,
drive: function(){
  console.log("Vroooooooooom");
}
};

let bus = new Vehicle(6,"red","150 bhp");

console.log(bus.drive());


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36.

Challenge: Understand Where an Object’s Prototype Comes From

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/understand-where-an-objects-prototype-comes-from

console.log(bus.drive());

bus.drive() is not returning anything, in JS a function that returns nothing implicitly return undefined, thus your console.log prints undefined.

Hope this helps :+1:

2 Likes

Shucks - I understand now. I will change console.log to return. got it. :smiley:

2 Likes