Contact Us

Let us know to help you out quickly!

 Admin User     Feb,08 2017

Correct Method to check if a Javascript function is Exists or not

Hi everyone,

Sometime you might want to call a function in Javascript but check whether the function exists or not before calling it. You can test if a function exists in Javascript by simply testing for the name of it in an if() condition.

But Note that you can't test for the function name by itself.
This will work but if the function doesn't exist the Javascript will error out.

 if(some_function_name) {} 

The Correct method is Like this

 if(window.some_function_name) {} 

It's a method of the window object so you need to test for window.some_function_name like so, where some_function_name is the name of the function you wish to test for.