Check if a Javascript Function Exists or Is Defined

digitalocean-bannerTo check if a Javascript function exists before calling it, try this:

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

I was surprised how hard it was to google the right answer to this…but I found it at CodingForums.

Please check out my other open source technology tutorialsFollow me @reifman.

  • Tommy

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

  • Bob

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

  • Darin Reid

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

    • jeffreifman

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

  • MattC

    very nice. Simple an to the point – just what I was looking for. :)

  • Neil Young

    Great stuff, worked a treat – thanks for this!

  • Lutz Zimmmermann

    Great stuff, thanks a million!

  • Ken

    Jason Bunting’s answer on Stackoverflow is probably the better way to do this.

  • Nicola


  • Rolly Schicker

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