How to tell if a JavaScript function is defined

Aaron Lee picture Aaron Lee · Sep 17, 2008 · Viewed 179.9k times · Source

How do you tell if a function in JavaScript is defined?

I want to do something like this

function something_cool(text, callback) {
    alert(text);
    if( callback != null ) callback();
}

But it gets me a

callback is not a function

error when callback is not defined.

Answer

Tom Ritter picture Tom Ritter · Sep 17, 2008
typeof callback === "function"