Purpose of declaring variable using arrow function

Hi,
I am on Functional Programming.
And I see something like this:

const prepareGreenTea = () => 'greenTea';

Why should someone declare a variable like that instead of justconst = 'greentea'; ? Is it of any advantages?

It’s a function. Same as writing function prepareGreenTea () {. Maybe in some real code you want to pass some data into the function, or maybe you want to do some calculations at the point it is called. Just anything you would use a function for (vs a simple variable assignment).

You need to bear in mind that this challenge tries to be the simplest possible case, but that it’s also trying to show that you can pass functions in exactly the same way as any other value in JS, that they aren’t anything special.

2 Likes

This is exactly what I was wondering, and I’m glad you were able to answer. I started doing some research to see if there is a benefit to declaring a variable as a return value in a function instead of declaring a variable directly, and the only use case would be if you needed to perform an action on the variable, like you stated.

@thangible, I found this article on arrow functions for beginners that goes into some examples on how and why arrow functions are useful. The first example is most related to your question I believe. You can see how short and simple an arrow function can be when returning a single variable, but performing an action on it. So essentially, no there is no benefit to declaring a variable the way you asked about, it’s just an example for the sake of teaching in this course. But, you can see how returning a variable *2 or variable + 2 can be super concise using arrow functions.

1 Like

Thank you for the info.
I was just thinking that there could be some purpose behind that.

Note that there is an actual genuine usecase here, and this challenge actually makes use of it. Say you have something you want to delay happening or delay using: just wrap it in an function that takes no arguments. You can now pass that function around, and nothing will happen until you actually execute it (at which point you get whatever is inside it). This called a “thunk” (the wiki is quite technical, but it’s a very simple thing, and the example under Applications -> Functional Programming is in ES6 and should be immediately understandable)

1 Like

So, if I am understanding this correctly…

A “thunk”, or an anonymous function is a way of saying “I need this value later, but it will cost too much processing or memory to calculate now, so I will only calculate it when I call it later”?

So then you have a function with a value that you can call at any time through the program, but it only runs when called. As opposed to assigning a variable some expensive calculation like the example of square roots, which would be calculated as soon as you assigned it.

Yep, that could be a usecase for sure. It’s not a complex thing: it can be done in almost any language, so it’s pretty common. Giving it a special name is maybe counterproductive, as it makes it seem special. Whereas it’s just a simple technique that I guess most of the time people use without realising that it has some formal properties

2 Likes