How to Check If a Function Exists in JavaScript

104 8
    • 1). Open the Web page document that contains the JavaScript code in your default text editor or a text-based HTML editor.

    • 2). Click to place your cursor on the line in the JavaScript code where you want to check if a function exists.

    • 3). Type "if(typeof functionName == 'function') {" then press "ENTER." Replace "functionName" with the name of your function (do not include parenthesis). This line of code opens the "if" statement and inserts the "typeof" operator. The function is then checked to see if it has been defined as a function in the previous code.

    • 4). Type "functionName;" then press "ENTER." Replace "functionName" with the name of your function. This line of code calls the function, if it is found to exist in the previous lines of code.

    • 5). Type "}" to close the statement.

    • 6). Click "File" and "Save" to save the Web page document.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.