Check if a Javascript Function Exists or Is Defined
To 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.
Technorati Tags: javascript, function defined, function exists, function existence

how about if the function name is in a variable like this:
var funcName = 'myFunc';
then i want to check if the function stored in funcName exists:
if (typeof eval(funcName) == 'function') {
this gives an error in:
myFunc is not defined
in firebug plugin for firefox
Posted by: andho | Sep 24, 2007 at 01:44 PM
Of course eval() cannot evaluate this variable, because it might be undefined. :)
But it works in this way:
if (eval("typeof " + funcName + " == 'function'")) {
}
Posted by: steve | Oct 03, 2007 at 06:15 AM
I solved the 'function name is in a variable'-problem with the code below:
if (typeof funcName == 'string' &&
eval('typeof ' + funcName) == 'function') {
eval(funcName+'()');
You have to eval the typeof and not typeof the eval.
Posted by: Søren Lund | Oct 11, 2007 at 05:09 AM
Soren,
if typeof funcName == 'string'
then eval('typeof ' + funcName) == 'function')
will the same as eval('string')
and you will get the error saying 'string' is undefined
Posted by: Ketan Solanki | Jul 02, 2008 at 04:16 PM
exactly what i needed, works beautifully.
tiny caveat when trying to test the existence of a function off of a jquery selector. have to select first...
if(typeof $("#datePicker").datepicker == 'function')
Posted by: jon | Oct 08, 2008 at 10:29 AM
Is there a way to check the same thing through php? For example I want to check if the scrptalicious blind function exists before hiding an html element.
Posted by: db0 | Oct 19, 2008 at 05:11 AM
Thanks! very fruitful post
Posted by: Ehsun | Nov 06, 2008 at 11:58 AM
How often are you calling functions that don't exist? Sounds a little overdefensive to me :)
Instead, try:
myFunction && myFunction.call && myFunction() || otherFunction();
This eliminates an unneeded "if()" closure.
Posted by: ken neville | Dec 02, 2008 at 08:09 PM
Short and to the point, and it worked perfectly! Much appreciated!
Posted by: Thanks! | Dec 17, 2008 at 02:49 PM
Why not just use...
if(document['funcName']) // call function
Posted by: Joe | Jan 13, 2009 at 09:30 AM
First piece of code I tried, and it worked perfectly. Many thanks.
Posted by: Lachlan | Jan 26, 2009 at 03:44 AM
Ken,
That will still call otherFunction() if myFunction() doesn't return truthy.
However, I like the elegance. Is there a way to check for existence only in this way; regardless of the return value for the function?
Posted by: Tavish | Feb 15, 2009 at 04:04 PM
Thanks man, if I was a chick I'd send a pic of my titties for thanks!
Posted by: Big Dirty | Sep 14, 2009 at 11:11 PM
Hey Dude !!!
Thanx a lot for this ... was lookin all over the internet for this solution !!!
Posted by: GreySkull | Sep 17, 2009 at 11:49 AM
Looks like a pretty simplistic solution. Glad I found this post, as I've been having a rough time trying to figure out how to do this recently. Many thanks!
Posted by: Phillip | Oct 28, 2009 at 06:25 PM
Thanks. Very good solution!
Posted by: Deanit | Nov 27, 2009 at 12:58 AM
If you just need to check if a function exists: you can just use if(window.function_name) [if the function is not contained in a class] OR if(className.function_name) [if the function is supposed to be in a class],
Remember not to include the "()" while checking :)
Posted by: rajesh sharma | Feb 02, 2010 at 01:01 PM
IE seems to return "object" sometimes, even for functions.
Posted by: Matt Densley | Mar 08, 2010 at 07:32 AM
How about using try{..}catch(e){..} it may be more suitable in many situations.
Posted by: Adr | Mar 16, 2010 at 08:30 AM
@jon:
Not true, a special jQuery function is pretty much always introduced using $.fn
so instead of $("#bla").myFunction() use $.fn.myFunction() to test if it's a function
Posted by: TeMc | Apr 06, 2010 at 06:59 AM