Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: what does function(_) mean

I'm going through the bacon.js slide at: http://raimohanska.github.io/bacon.js-slides/1.html

In the 1st line of the 2nd block, it says:

function always(value) { return function(_) { return value } }

what does function(_) mean?

like image 850
tldr Avatar asked May 28 '14 05:05

tldr


People also ask

What does function () mean in JavaScript?

In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want. A JavaScript function can be defined using function keyword.

What does function * mean JavaScript?

The function* declaration ( function keyword followed by an asterisk) defines a generator function, which returns a Generator object.

What does {} mean after the function JavaScript?

It's usually used to work around a quirk in the JavaScript syntax. This gives a syntax error: function() { }(); It's read as a function declaration (like function foo () {} ), rather than a function expression.

What does '$' mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.


1 Answers

In this case _ is just a function parameter - a single underscore is a convention used by some programmers to indicate "ignore this binding/parameter".

Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely. Such a "throw-away" identifier is found more commonly in other languages, but consider a case like arr.forEach(function (_, i) {..}) where _ indicates the first parameter is not to be used.

like image 155
user2864740 Avatar answered Oct 28 '22 11:10

user2864740