How to check a function is defined in JavaScript ?
The job is to identify whether a function is defined or not. The JavaScript typeof operator is used to solve the problem described below:
Javascript typeof Operator: This operator can be used to find the type of a JavaScript variable. This operator returns the type of a variable or an expression:
Syntax:
typeof var
Parameter: It contains single value var which is a Javascript variable.
Return value: It returns the type of a variable or an expression:
Example 1: This example checks the type of the function, If it is function then it is defined otherwise not defined by using typeof operator.
<!DOCTYPE HTML> < html > < head > < title > How to check a function is defined </ title > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;" > </ p > < button onclick = "gfg_Run()" > Click here </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;" > </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Function named 'fun' which is not defined"; function gfg_Run() { var defined = 'Not defined'; if(typeof fun == 'function') { defined = "Defined"; } el_down.innerHTML = defined; } </ script > </ body > </ html > |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example checks the type of the function, If it is function then it is defined otherwise not defined by using typeof operator by creating a function.
<!DOCTYPE HTML> < html > < head > < title > How to check a function is defined </ title > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p id = "GFG_UP" style = "font-size: 16px; font-weight: bold;" > </ p > < button onclick = "gfg_Run()" > Click here </ button > < p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;" > </ p > < script > var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Function named 'fun' which is defined"; function isFunction(possibleFunction) { return typeof(possibleFunction) === typeof(Function); } function fun() { } function gfg_Run() { var defined = 'Not defined'; if(isFunction(fun)) { defined = "Defined"; } el_down.innerHTML = defined; } </ script > </ body > </ html > |
Output:
-
Before clicking on the button:
-
After clicking on the button:
- How to check a variable is of function type using JavaScript ?
- PHP | defined() function
- How to find out where a function is defined using PHP ?
- Substring check in JavaScript
- How to check a key exists in JavaScript object?
- How to check two elements are same using jQuery/JavaScript ?
- How to check if date is less than 1 hour ago using JavaScript ?
- How to check object is an array in JavaScript?
- Check a number is Prime or not using JavaScript
- Check if an array is empty or not in JavaScript
- How to Check/Uncheck the checkbox using JavaScript ?
- JavaScript | Check if a variable is a string
- How to check if a string is html or not using JavaScript?
- How to check an object is empty using JavaScript?
- How to check if a variable is an array in JavaScript?
<
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.