Check if function is defined

The easiest way to check if a specific function is defined / exits in the current context.


Libraries

Code Snippet
if (typeof funcName === "function") { 
    // funcName is defined.
} else {
    // Use another funcName instead.
}


Implementation

Use the function above like<code>if (typeof JSON.parse === "function") { // Can parse JSON with JSON.parse }</code> or <code>if (typeof String.prototype.startsWith === "function") { // Can use String.startsWith }</code>





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Basics



    Informations

    posted on 2016-09-22 16:23:26

    viewed 118 times

    snippet's UUID is 57e3e95eace31

    Similar Snippets