Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

jquery
communitycreator

How to check if a variable is a function in jQuery

Programming Bytes

Overview

We can use the isFunction method to check whether a variable is a function or not.

Syntax

jQuery.isFunction( value )

Arguments

The variable to be checked is passed as an argument to this method.

Return value

This method returns True if the argument is a function. Otherwise, it returns False.

Code example

The code written below demonstrates how we use the isFunction method:

Console
Using the "isFunction" method to check if a variable is a function

Code explanation

Line 6: We include the jQuery file.

Lines 9–10: We create an object obj and use the isFunction method to check if obj is a function. The isFunction method returns False, because obj is not a function.

Lines 13–14: We create a variable val with a value of 10 and use the isFunction method to check if val is a function. The isFunction method returns False, because the val is not a function.

Lines 16–19: We create a function with the name hi and use the isFunction method to check if hi is a function. The isFunction method returns True, because hi is a function.

RELATED TAGS

jquery
communitycreator
RELATED COURSES

View all Courses

Keep Exploring