-2

Why won't I get both the return statement as well as console.log statement results?

The console.log outputs "Emily" but it won't output the text "says, that excessive coffee is awesome!"

function coffeeDrink(){
    return this.name;
    console.log("says, that excessive coffee is awesome!")
}

var literalMallory = {
    name         : "Mallory",
    favDrink     : coffeeDrink
}

var literalEmily = {
    name        : "Emily",
    favDrink    : coffeeDrink
}

coffeeDrink.call(literalEmily) //outputs "Emily"

I am expecting it to say: "Emily says, that excessive coffee is awesome!"

j08691
  • 204,283
  • 31
  • 260
  • 272
Donna
  • 7
  • 6

1 Answers1

0

you need

function coffeeDrink(){
    console.log(this.name + " says, that excessive coffee is awesome!");
    return this.name;
}

return statement exits the execution, putting it as last statement will make console.log work and also it will return the name to calleee

Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20
  • hey thank you! I am a beginner, and yes, I only knew that return will return something but I had no idea that it will exit the function immediately, even though I might have learned it a few months back. Thank you so much – Donna Oct 01 '19 at 15:51