Why does a function have no body?

I’m learning javascript and saw this code:

const myFunc = () => "value"

why does it use function(without body) instead of normal variable here?

The above uses arrow function syntax. It is the equivalent of:

function myFunc() {
  return "value";

Thanks man. In this case why do we need a function while there is no logic inside it?

It does have logic.

It is a getter method that accepts no parameters (what you are mistaking as no body) and returns the string-value “value”.

1 Like

This specific function’s code is rather basic and is probably just for demonstration purposes. Was this something you found on the FCC site?

There are situations when you might want a function which just return a very specific value. You would want such a function if you need to return a specific value multiple times for different reasons in other parts of your code. You can create this function and then call it whenever you need to. By having it in a function, this would allow you to change just the return value in the function in one place if you ever decide the need to change the value. Otherwise, you would have to change this value multiple places throughout your code.

1 Like

Yes it’s in a FCC lesson

I got it, thank you so much.