FORSMILE
JA
JavaScript2020/07/20

[JavaScript] How to Check if a Value is a Function

While reviewing code at work, I came across the following snippet where a callback function was being set.

Back to Blog

While reviewing code at work, I came across the following snippet where a callback function was being set.

css
if($.isFunction(callback)){
    callback();
}

You can also write this in plain JavaScript. To check if a passed value is a function, you can use the 'typeof' operator.

javascript
function(callBack)    
{
    ...
    if(typeof callBack == 'function') {
        callBack();
    }
}

jQuery's $.isFunction method returns a boolean value.

In general, I believe using 'typeof' is a good way to check types.

RECOMMENDED JAVASCRIPT BOOKS

JavaScript has an abundance of online resources, and Google searches are highly effective, so you might not feel the need to read books.

However, for beginners, I believe reading one simple book can significantly deepen their understanding. While there are good reference books available, a quick Google search often allows for easy copy-pasting, making physical books potentially less necessary for such purposes.

Enlightenment! JavaScript — Learning the Essence of JavaScript from Language Specifications

Highly recommended! You'll learn aspects of JavaScript that typically don't surface in Google searches. After finishing this book, I truly felt my coding abilities had advanced a level.

📦
Amazon で関連書籍・ツールを検索
JavaScript programming book
Amazonで探す →(アソシエイトリンク)