Categories
JavaScript Answers

How to test if value is a function with JavaScript?

Spread the love

Sometimes, we want to test if value is a function with JavaScript.

In this article, we’ll look at how to test if value is a function with JavaScript.

How to test if value is a function with JavaScript?

To test if value is a function with JavaScript, we can use the typeof operator.

For instance, we write

typeof document.getElementById("myform").onsubmit === "function";

to check if the document.getElementById("myform").onsubmit property is a function by checking if typeof document.getElementById("myform").onsubmit returns 'function'.

If it does, then it’s a function.

Conclusion

To test if value is a function with JavaScript, we can use the typeof operator.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.