2
const post = text => (
   ....
)

and

const post = text => {
    ....
}

I'm new, sorry if this a stupid question, I've searched some articles, but didn't get it. Could someone help to explain

Omkar76
  • 1,317
  • 1
  • 8
  • 22
pixy
  • 61
  • 9

2 Answers2

4
const post = text => (
   ....
)

This arrow function expects an expression or single statement in parentheses. The result of expression will be returned when you call the function. No need to write return explicitly.

example:

const isOdd = num => ( num % 2 == 1 );

The second arrow function expects a function body. If you don't explicitly return, undefined will be returned.

const post = text => {
    ....
}

example:

const isOdd = num =>{
   return num % 2 == 1;
}

In the first form you don't always need () around expression, but it is required when when you return an object literal.

const Point = (x,y) => {x,y};
console.log(Point(0,0)); //undefined
const Point = (x,y) => ({x,y});
console.log(Point(0,0)); //{ x: 1, y: 0 }
Omkar76
  • 1,317
  • 1
  • 8
  • 22
0

first arrow function means return a value (what is return) second arrow function means your function that you wants to define {define your function} for more description follow this sample:

const post = text => (text) // return text

const post2 = text =>{ //define your function
  return (text)
}

console.log(post("hello post"));
console.log(post("hello post2"));
Mehrzad Tejareh
  • 635
  • 5
  • 21