squareNums() why can't I include the braces {}?

Hello, I finished working on the squareNums() project.

Check out line 3 of this code which works:

const numbers = [2, 7, 9, 171, 52, 33, 14];
const toSquare = num => num * num
const squareNums = (arr) => arr.map(toSquare)   // this is the line confusing me
console.log(squareNums(numbers))
// works!  I get: [ 4, 49, 81, 29241, 2704, 1089, 196 ]

compare the above with:

const numbers = [2, 7, 9, 171, 52, 33, 14];
const toSquare = num => num * num
const squareNums = (arr) => {arr.map(toSquare)}  // the braces {} cause it to fail
console.log(squareNums(numbers))
// FAILS!  I get: undefined

QUESTION: Why do I have to exclude those braces?
I thought “Concise Body Arrow Functions” was a refactor option, not a requirement.

Thanks Guys,

1 Like

One suspects it is because there may be no implicit return when the braces are present.

(arr) => arr.map(toSquare);

vs.

(arr) => {
  return arr.map(toSquare);
}
2 Likes

That was a darn good theory so I tested it and you are correct.

const numbers = [2, 7, 9, 171, 52, 33, 14];
const toSquare = num => num * num
const squareNums = (arr) => {
  return arr.map(toSquare)
}
console.log(squareNums(numbers))

works as intended…

Thank you~

1 Like