We can use the isArray
method to check if an object is an array or not.
jQuery.isArray( obj )
obj
: This is the object to be tested.
The isArray
method returns true
if the argument passed is an array. Otherwise, false
is returned.
The code below demonstrates how to check if an object is an array using jQuery.
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 arr
and call the isArray
method with the created array. The isArray
method will return true
.
Lines 10 and 11: We create an empty object obj
and call the isArray
method with the created object. The isArray
method will return false
because the argument passed to the isArray
method is not an array.