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.

Console
Use the isArray method to check if an object is an array

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 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.

Free Resources