-1

When using map function in js, mostly I see people using curly brackets but sometimes they use small brackets after arrow. Why is that ?When using curly brackets, I get error sometimes

Zac Anger
  • 6,983
  • 2
  • 15
  • 42

1 Answers1

2
foo.map((x) => (x))

Is equivalent to:

foo.map(function (x) {
  return x
})
foo.map((x) => { x })

Is equivalent to

foo.map(function (x) {
  x
})

When wrapping in parenthesis (which are unnecessary but sometimes a useful visual aid), the right-hand side is being immediately returned. When using curly braces, you're beginning a new block which can contain any number of statements (and should also include a return, unless you're intentionally trying to get an array of undefineds).

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • Worth also mentioning that sometimes you need to use both in order to return an object literal: `foo.map((x) => ( { bar: x, hello: "world" } ))` – Dmiters Nov 30 '22 at 07:13