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.