57

In JS, there is shorthand for an empty object which is {}. Is there shorthand for an empty function in JS?

The reason being, as functions are first-class objects, we use them as arguments more often, but passing in an empty function is at best ugly.

var foo = baz(function(){});

In order to declare a function, at somepoint we have to declare function(){}.

I would like more Node.js APIs to require a (callback) function to be passed, so the API doesn't deceivingly look synchronous. Perhaps one step in that direction would be to create shorthand for empty placeholder functions.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 3
    How about creating a function and referencing it in a variable for reuse? –  Jun 02 '15 at 00:26
  • 2
    I guess you could enable ES6 features and use arrow function syntax `a=>0` –  Jun 02 '15 at 00:32
  • 1
    i use `Boolean` for "void" callbacks to avoid throwing, since it can be passed anything. – dandavis Jun 02 '15 at 00:35
  • @dandavis can you clarify? – Alexander Mills Jun 02 '15 at 00:38
  • ex: `var foo=baz(Boolean);` won't throw because of a missing function, or because you passed the function some argument/type it didn't like. It's also fast and has no side-effects. – dandavis Jun 02 '15 at 00:39
  • "*so the API doesn't deceivingly look synchronous.*" - What do you mean? If an API is asynchronous and has a result that you care for, then you wouldn't use an *empty* callback anyway. And calling a function for whose result you don't care is very rare. Can you show an example? – Bergi Jun 02 '15 at 00:44
  • @Bergi you are completely missing the point. Some aync APIs don't require the developer to pass a callback. This then makes the API look synchronous. Then developers pull their remaining hair out trying to figure out why return values are undefined etc. If the API simply threw an error if no callback was passed, then it would save a lot of headache. All the dev would have to do is pass an empty function. Now the dev knows the API is async. – Alexander Mills Jun 02 '15 at 00:50
  • @Bergi: i've always felt that fs.append's callback is silly if you don't need flow and just don't want to use sync (https://nodejs.org/api/fs.html#fs_fs_appendfile_filename_data_options_callback ). since it creates the file if missing, there's not a whole lot that `err` will ever be... – dandavis Jun 02 '15 at 00:54
  • 1
    @dandavis: OK, logging is a good example for a side effect that I don't really care about, though I'd usually expect that to be in a single `.log` function that handles everything. – Bergi Jun 02 '15 at 01:00
  • @Bergi for the async functions for which you don't care about the result, this is when a shorthand for an empty function would be quite 'useful' in that we might bring about a renaissance in Node.js APIs that required callbacks, even if they were empty. – Alexander Mills Jun 02 '15 at 01:03
  • 1
    @AlexMills: Hm, I'm still not convinced. Throwing if there is no callback might be a good idea, but that developer who wonders about `undefined` return values would not use an empty callback either - he wants the result. And I strongly oppose to make APIs "looking asynchronous" just because they take a callback - there are [enough synchronous callback-taking methods](http://stackoverflow.com/q/19083357/1048572) that return values. Try to use promises, there you can be explicit about asynchrony with `.then` or `await` (in ES7) – Bergi Jun 02 '15 at 01:05
  • I see, I wonder if there are any conventions with function names or what not that establish sync/async functions. it seems sorry state of affairs that there are none yet. – Alexander Mills Jun 02 '15 at 01:24
  • is using `void(0)` a bad practice for this? – bugovicsb Aug 28 '18 at 10:27
  • can `void(0)` be called? is it callable? – Alexander Mills Aug 28 '18 at 23:57

4 Answers4

55

No, there is not. With ES6, you might be able to use an arrow function: ()=>{} which is a bit shorter.

If you really need this very often (you shouldn't?!), you can declare one yourself:

function noop(){}

and then refer to that repeatedly. If you don't want to clutter your scope, you can also use the Function.prototype function (sic!) which does nothing but constantly return undefined - yet that's actually longer than your function expression.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
31

ES6 one could be:

   const noop = () => {};

Be careful if using Babel as it compiles differently - instead you can explicitly return something:

   const noop = () => ({});

See this tweet as to why the curly braces are important: https://twitter.com/_ericelliott/status/601420050327711744?lang=en

sidonaldson
  • 24,431
  • 10
  • 56
  • 61
  • 1
    Brackets or curly braces? I always thought these `[]` were brackets – Alexander Mills May 10 '19 at 18:41
  • @AlexanderMills just a spelling mistake :) – sidonaldson May 13 '19 at 08:36
  • 4
    The curly braces are important, but the parentheses around them make it a non-dummy function: it *returns* an empty object like this. Better remove the parentheses so that the braces represent an empty statement block which is what `function() {)` has. – trincot Jul 10 '19 at 07:35
  • 1
    Your function is different from `function() {}` because it returns `{}` as another commenter has already pointed out. – binki Aug 18 '19 at 01:09
  • Thanks for the downvotes. I've clarified in the post – sidonaldson Aug 22 '19 at 08:14
4

You could try the following if you are trying to avoid curly brackets:

const f = () => undefined;
user3163495
  • 2,425
  • 2
  • 26
  • 43
3

If you use lodash, there is a noop method.

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40