Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this underscore mean in node js

I saw the following line in a node js code base.

_ => { return resolve(abc);}

Can any one give me some hint on what this _ means? I know => is fat arrow function. This line is from a snippet of promise.

like image 300
Ziyang Zhang Avatar asked Feb 19 '26 10:02

Ziyang Zhang


2 Answers

At the coaxing of a discussion elsewhere in this question, I'll turn my comments into an answer.

First off, I assume you aren't asking about arrow functions in general, but are just curious about the _ in your code sample.

There is a convention (used by some) to declare and name a parameter to a callback function that you know is passed, but you are not going to be using with an underscore as a sort of placeholder. It's a signal to people reading the code that there is an argument being passed to this callback, but you don't plan on using it in this particular invocation. It's presence is not functional, but more for

Now, without knowing what was in the head of the developer who wrote the line of code you asked about, we can't know for sure what the underscore means, but it does appear to match that previously described convention. There is one argument to the callback and it is not used in the implementation of the callback.

So, if our guess that this is a use of that convention is true, then in the arrow function that you show:

_ => { return resolve(abc);}

It is likely expecting one argument, but this particular use of that callback does not plan on using it, thus they named it with an _ just to signal that.


Now, there is no particular reason in Javascript that the callback needs to even define a single argument like this that be used. The callback could just as well have been defined as:

() => { return resolve(abc);}

since there's no requirement in Javascript that you declare arguments you aren't going to use.

If the callback took two arguments and it was the first one that you weren't going to use, then it does have to be declared as something in order to allow access to the second argument:

(_, secondArg) => { return resolve(secondArg);}
like image 140
jfriend00 Avatar answered Feb 20 '26 22:02

jfriend00


It means that the arrow function argument is not used. They use _ as argument name for the arrow function.

like image 45
yeiniel Avatar answered Feb 21 '26 00:02

yeiniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!