+ 1

Why is "undefined" in the output?

https://code.sololearn.com/WgSieH3Z4rA0/?ref=app I am trying to print a statement using a function that contains a property of the object. The output is there but it's followed by an undefined in the next line each time i execute the function.

27th Aug 2020, 4:26 AM
Deven
Deven - avatar
2 Respostas
+ 3
this is cuz u wrote console.log(obj1.proud()) and you are already console.logging inside the function so its like console.log(console.log("something")) so its returning undefined. Two ways to solve it: 1. use a return statement at the end of function: return this.name instead of console.log(this.name) 2. Don't do the above method and just remove console.log() where u call the function, just call it directly. 1st ones better coz u can store its output inside variables too, coz its returning something.
27th Aug 2020, 4:47 AM
maf
maf - avatar
+ 1
Thank you very much 😄😄
27th Aug 2020, 5:41 AM
Deven
Deven - avatar