How to check if function exists in JavaScript?

How to check if function exists in JavaScript?

I followed this guide to create a new JS to flash communication.
My code is
function getID( swfID ){
if(navigator.appName.indexOf(“Microsoft”) != -1){
me = window[swfID];
me = document[swfID];

function js_to_as( str ){

However, sometimes my onChange does not load. Firebug errors with

me.onChange is not a function

I want to degrade gracefully because this is not the most important feature in my program. typeof gives the same error.
Any suggestions on how to make sure that it exists and then only execute onChange?
(None of the methods below except try catch one work)


Solution 1:

Try something like this:

if (typeof me.onChange !== "undefined") { 
    // safe to use the function

or better yet (as per UpTheCreek upvoted comment)

if (typeof me.onChange === "function") { 
    // safe to use the function

Solution 2:

I had this problem.

if (obj && typeof obj === 'function') { ... }

kept throwing a reference error if obj happened to be undefined.

In the end I did the following:

if (typeof obj !== 'undefined' && typeof obj === 'function') { ... }

A colleague pointed out to me that checking if it’s !== 'undefined' and then === 'function' is redundant of course.


if (typeof obj === 'function') { ... }

Much cleaner and works great.

Solution 3:

If you’re using eval to convert a string to function, and you want to check if this eval’d method exists, you’ll want to use typeof and your function string inside an eval:

var functionString = "nonexsitantFunction"
eval("typeof " + functionString) // returns "undefined" or "function"

Don’t reverse this and try a typeof on eval. If you do a ReferenceError will be thrown:

var functionString = "nonexsitantFunction"
typeof(eval(functionString)) // returns ReferenceError: [function] is not defined

Solution 4:

How about:

if('functionName' in Obj){


var color1 = new String("green");
"length" in color1 // returns true
"indexOf" in color1 // returns true
"blablabla" in color1 // returns false

or as for your case:

if('onChange' in me){

See MDN docs.

Solution 5:

Try typeof — Look for 'undefined' to say it doesn’t exist, 'function' for a function. JSFiddle for this code

function thisishere() {
    return false;
alert("thisishere() is a " + typeof thisishere);
alert("thisisnthere() is " + typeof thisisnthere);

Or as an if:

if (typeof thisishere === 'function') {
    // function exists

Or with a return value, on a single line:

var exists = (typeof thisishere === 'function') ? "Value if true" : "Value if false";
var exists = (typeof thisishere === 'function') // Returns true or false

Solution 6:

Didn’t see this suggested:
me.onChange && me.onChange(str);

Basically if me.onChange is undefined (which it will be if it hasn’t been initiated) then it won’t execute the latter part. If me.onChange is a function, it will execute me.onChange(str).

You can even go further and do:

me && me.onChange && me.onChange(str);

in case me is async as well.