How to check a function is defined in JavaScript ? - GeeksforGeeks

24 May, 2019
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:<My Personal Notes
arrow_drop_upSave
<
javascript-functions
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript