SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Correct way to check if JS function exists?

    What is the correct way to check if a JS function exists?

    I'm told that typeof works but isn't a very good way to do it.

    Also, this doesn't work:
    Code:
    if( function_name ) {
    function_name();
    }

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    As far as I know, typeof is both good and standard. Did the person who said otherwise give a reason?

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, which is why I thought to check.

    Considering 'typeof' works, but 'if(function_name)' doesn't then typeof is good enough for me!

  4. #4
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Do you have a test case where if(function_name) doesn't work?
    Code:
    var yep = function() {}
    if (yep) { console.log('yep') }
    else { console.log('nope') }

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you don't use typeof then you will not know if a function by that name exists - the name might be being used for something else. It might even be being used but contain a value that returns false.


    Code:
    var yep = {} // an object instead of a function
     if (yep) { console.log('yep') } // incorrect response
     else { console.log('nope') }
    The safest way to test is:


    Code:
    if (typeof yep==='function')
    That way if the name exists but isn't a function your subsequent processing will still work correctly.

    As an actual example of where just testing the name wouldn't work - I have a calendar script where you can either supply an object containing a list of holiday dates or supply a function that will generate the object. By testing whether the value passed is a function or not the code can run the function to generate the object. Simply testing if the field exists wouldn't work since both an object and a function would return true for that test.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by markbrown4 View Post
    Do you have a test case where if(function_name) doesn't work?
    Sure do.

    Code:
    if (yep) { console.log('yep') }
    else { console.log('nope') }
    This results in an error: "yep is not defined"

    Of course, really this only happens because "yep" wasn't declared with "var". So instead of typeof, you could also do this.

    Code:
    // if yep exists, then re-declaring it won't do any harm
    // if yep doesn't exist, then we declare it for the first time
    var yep;
    
    if (yep) { console.log('yep') }
    else { console.log('nope') }

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    This results in an error: "yep is not defined"
    In strict JavaScript the code would just fail to run completely (since in strict JavaScript all variables MUST be defined) and so wouldn't generate the error.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •