0

When I log this to the console it shows up as undefined. However, when I remove the braces in the function it logs the desired result. I'm confused -- aren't we always supposed to include curly braces in a function? Thank you all.

const shoutGreetings = arr => {
  arr.map(word => word.toUpperCase() + '!')
};

const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
console.log(shoutGreetings(greetings))
isherwood
  • 58,414
  • 16
  • 114
  • 157
Techguru
  • 41
  • 1
  • 3
  • You can omit the braces in an arrow function if it's a single expression. Then it automatically returns the value of that expression. – Barmar Oct 08 '21 at 21:08
  • 1
    But if you have braces, you need to use `return` if you want it to return something. – Barmar Oct 08 '21 at 21:08
  • @Barmar is right. In one case you are (automatically) returning the result of `map` and in the other, you are not, as this (automatic) returning stops when you introduce curly braces. – Krokodil Oct 08 '21 at 21:09

1 Answers1

1

Return the result of your map function: return arr.map(...

Krokodil
  • 1,165
  • 5
  • 19