Javascript : is given function empty?

Let's have a function call

function doSomethingAndInvokeCallback(callback){
    // do something

I can check if given argument is function if(typeof callback == 'function')

How can I discover, if given callback function is function and isn't empty?


    //nothing here



There is no totally reliable way to know if a function is empty because there are multiple kinds of functions in JS, some implemented with JS and some implemented with native code and you can't know for sure whether the function passed in does anything or not. If you want to limit the passed in function to only very simple JS functions, you could use the mechanisms outlined by other answers here (examining the source of the function). But, I would not recommend doing that in anything but a tightly controlled situation because there are lots of legal javascript ways to break that.

I would suggest that you should change the contract of your function arguments and have the caller pass null or not pass anything (which will make the argument undefined) rather than an empty function. Then, it will be very clear whether they intend to have a function called or not. If they then pass an empty function instead of null or undefined, they are getting the behavior that the interface of the function specifies. The caller can choose the desired behavior and you can implement your function in a more failsafe manner.

Also, one of your main suppositions in your question is not quite right. You cannot safely use typeof x == "function" to determine if something is a function as that will not work reliably in some older versions of IE for some types of functions. If you want to learn how to detect if something is a function at all, you can learn from jQuery here (even if you're not using it). jQuery has a function it uses internally all the time called jQuery.isFunction() that returns a bool. It uses that mostly for testing arguments to see if a function was passed.

Internally, it calls:

and then examines the result. If the result has "Function" in it, then test test parameter is a function.

So, using the same technique used in jQuery, you could build your own simple little isFunction routine like this:

function isFunction(test) {
    return("Function") > -1);

Of course, if you have jQuery available, you could just use it's own version:


When there are questions with potential cross browser compatibility issues, I find it instructional to look at how one of the big libraries solves the issue, even if you aren't going to be using that library. You can be sure that the libraries have been vetted against many browsers so a technique they are using is safe. You sometimes have to unwrap all their own internal routines they may use to figure out what they're really doing (which was the case for this function), but you can save yourself a lot of legwork.

You can see a working test bed for this here:

In modern browsers typeof fn === "function", but in older versions of IE, some functions give a typeof === "object" which is probably why jQuery uses this other method which does work in those older versions of IE.


It seems that you can define a function to retrieve the body of a function(1). I wrote a small (non-definitive) test of this:

Function.prototype.getBody =
  function() {
    // Get content between first { and last }
    var m = this.toString().match(/\{([\s\S]*)\}/m)[1];
    // Strip comments
    return m.replace(/^\s*\/\/.*$/mg,'');

function foo() {
    var a = 1, b = "bar";
    alert(b + a);
    return null;


One possibility is matching the .toString result against a regexp to get the function body, and then trim to check whether it has become an empty string:

var f = function foo()    { 


/^function [^(]*\(\)[ ]*{(.*)}$/.exec(
     f.toString().replace(/\n/g, "")
)[1].trim() === ""; // true

That ugly regexp does take care of spaces aroung named functions as well as extraneous spaces before the name and the opening brace. Spaces like in foo () do seem to be removed, so there is no reason to check for those.


You might be able to get this from .toString():

var blank = function(){};
var f = function(){};
var f2 = function() { return 1; };

f.toString() == blank.toString(); // true
f2.toString() == blank.toString(); // false

but this is really prone to error:

var blank = function(){};
var f = function(){ }; // extra space!
f.toString() == blank.toString(); // false

You could munge the strings a bit to try to overcome this, but I suspect this is very browser-dependent. I wouldn't actually try to do this in a production environment if I were you. Even if you normalize the whitespace, it still won't catch other no-op lines, including comments, useless var statements, etc. To actually address these issues, you'd probably need a whole tokenizer system (or a crazy regex).


You can't do it for a host function, but for others, you can fairly reliably do

function isEmpty(f) {
  return typeof f === "function" &&

This isn't efficient, but major interpreters implement toString for functions in such a way that it works, though it will not work on some interpreters for some empty-ish functions

function () { /* nothing here */ }
function () { ; }
function () { return; }

In some implementation you can just do a toString() on the function and get it's content. Though it contains comments etcetera.

var foo = function(){ /* Comment */ };


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.