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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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