How to check if a value is an array in jQuery
Overview
We can use the isArray method to check if an object is an array or not.
Syntax
jQuery.isArray( obj )
Arguments
obj: This is the object to be tested.
Return value
The isArray method returns true if the argument passed is an array. Otherwise, false is returned.
Code
The code below demonstrates how to check if an object is an array using jQuery.
Code explanation
Here is a line-by-line explanation of the above code.
-
Line 5: We include the jQuery file.
-
Lines 7 and 8: We create an array
arrand call theisArraymethod with the created array. TheisArraymethod will returntrue. -
Lines 10 and 11: We create an empty object
objand call theisArraymethod with the created object. TheisArraymethod will returnfalsebecause the argument passed to theisArraymethod is not an array.