Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a JavaScript function is defined

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.

like image 343
Aaron Lee Avatar asked Sep 17 '08 17:09

Aaron Lee


People also ask

Why is JavaScript saying my function is not defined?

You're Calling the Function Before It's Defined If the code that calls your JavaScript function precedes that function's definition in your HTML document, you will come across the function is not defined error.

How do you define a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)

How do you know if a variable is defined?

Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.

How do you know if a function exists?

Use an if Conditional Statement The code in the brackets will execute if the function is defined. If instead you just test the function without using the window object, for example as if(aFunctionName) , JavaScript will throw a ReferenceErorr if the function doesn't exist.

How to check if a function name has been defined in JavaScript?

To check if a particular function name has been defined, you can use JavaScript’s typeof operator: In our case, the typeof operator will return the string “undefined” because bad_function_call has not been defined.

How to check if a JavaScript function exists before calling it?

Check if a JavaScript function exists before calling it. To check if a particular function name has been defined, you can use JavaScript’s typeof operator: //Use the typeof operator to check if a JS function exists. In our case, the typeof operator will return the string “undefined” because bad_function_call has not been defined.

How to check if a variable is defined or not?

Knowing the possible states of variables, let's consider the techniques to find whether a variable is defined or not. The typeof operator determines the variable's type. typeof myVar can evaluate to one of the values: 'boolean', 'number', 'string', 'symbol', 'object', 'function' and 'undefined'.

How do I check if a variable is of type function?

With jQuery.isFunction() you may test a parameter to check if it is (a) defined and (b) is of type "function.". Since you asked for jQuery, this function will tickle your fancy. If you wish not to use jQuery for whatever reason, here's a barebones function based on code from Idealog that will check if the variable is of type function.


1 Answers

typeof callback === "function" 
like image 197
Tom Ritter Avatar answered Oct 07 '22 14:10

Tom Ritter