Check if a Javascript Function Exists or Is Defined

It’s always frustrating when you get an error trying to call a function that hasn’t been defined but there’s an easy way  to prevent this. To check if a Javascript function exists before calling it, try this:

if (typeof yourFunctionName == 'function')
{
 yourFunctionName(); 
}

Here are some related stories you may also be interested in:

Also, check out Publishing with WordPress, it’s full of useful and timely WordPress guides,  or follow me on Twitter @reifman.

Bloggers: WP Engine has increased their referral payments to $200 each! Try out their WordPress hosting.

16 Comments

  1. Finally! I’ve been looking for way too long for just this easy a solution! Thank you very very much!!!!

    Reply

  2. terrific, you are on 1st place in google for javascript check it function exists, thank you

    Reply

  3. Hey neighbor, imagine my surprise when I googled this tonight. See you at coffee!

    Reply

    1. HA! We should cowork 🙂 Email me your email again … as I can’t seem to find it.

      Reply

  4. very nice. Simple an to the point – just what I was looking for. 🙂

    Reply

  5. Great stuff, worked a treat – thanks for this!

    Reply

  6. Lutz Zimmmermann May 17, 2013 at 11:29 am

    Great stuff, thanks a million!

    Reply

  7. Jason Bunting’s answer on Stackoverflow is probably the better way to do this. http://stackoverflow.com/questions/85815/how-to-tell-if-a-javascript-function-is-defined

    Reply

  8. Sounds like y’all are real happy with the solution…now for us dummies….where do you enter(type) all that good stuff?

    Reply

  9. Man you have been a great help, thank you for this trick

    Reply

  10. great solution thx!

    Reply

  11. Try this.

    isFunc = function(func){ return typeof window[func] === “function”;}

    much better.

    returns true or false, func is the string name

    var x = isFunc(“myFunc”);

    x will be true or false depending on if myFunc is real or not.

    Reply

  12. The solution here will break your script if variable or function not exists so your solution doesn’t make sense at all. Use .yourFunctionName instead, it’s safer especially for older browsers. In this case this is the window object, window.yourFunctionName. All global functions will be attached to the window object. Cheers!

    Reply

  13. Very useful! Got me out of a jam when I was calling a Javascript function from JavaFX. The target Javascript may or may not have implemented the hook and was dumping harmless stack traces. I started to get lazy and bury the exception and add a TODO to come back and address it. However, your solution worked great. Buried exceptions are one of the worst anti-patterns around.

    Thanks!

    Reply

  14. Here i have a var timer = setInterval(function ()
    {

    }, 60 * 1000);

    when i checked into console, it return flase.

    function TimerService()
    {
    if(typeof timer == ‘function’){

    alert(“cleartimer”);

    clearInterval(timer);

    }

    var lastDigestRun = new Date();
    lastDigestRun = Date.now();
    var s = lastDigestRun + 3 * 60 * 1000;
    // for reference
    document.getElementById(“demo”).innerHTML = new Date(s);
    console.info(“Your logout time +” + new Date(s));

    var timer=setInterval(function () {
    var now = Date.now();
    var displaytime = now – lastDigestRun > 2 * 60 * 1000;

    if (now – lastDigestRun > 2 * 60 * 1000) {
    load();
    }

    if (now – lastDigestRun > 3 * 60 * 1000) {
    alert(“logout”);

    }
    }, 60 * 1000);
    }

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *