How to check if a JavaScript function is defined

I encountered a small issue on javascript while doing a small hack for a VBulletin plugin. Sometimes a javascript function was included, sometimes not.
So I needed a way to check if that function was or wasn’t defined so I can call it safely (without any error message). In case you ever need something like this .. here is the code 🙂

JAVASCRIPT [Show Plain Code]:
  1. if(typeof functionNameToCheck == ‘function’) {
  2.         //you can now safely call your function
  3. }

2 comments on “How to check if a JavaScript function is defined

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.