Javascript Practice

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/lessons/intermediate-javascript-coding-challenge/exercises/shout-greetings

From this lesson, I was wondering what’s the difference between

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

and

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

The first one is giving me undefined answer and I’m pretty sure everything else is the same except how Introduced the function.

here:

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

is shorthand is used, when the body of the arrow function is a single line, the curly brackets and return keyword can be omitted, which will result in an implicit return

here:

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

you use the curly brackets, so you also need to use the return keyword

1 Like

Thank you for the help!

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