Check variable is Array using jQuery

Today for one of my functionality, I need to check whether the Java script variable is an array or not. jQuery provides a method "jQuery.isArray()" to check whether the element is Java script array or not.

jQuery.isArray() returns a boolean value that indicates whether the object is a JavaScript array or not. It takes the variable name as an argument and returns the result back to caller. See below jQuery code.

   var x = [];
   var y = [1,2,3];
   var z = 1;
   alert('x is an array: ' + $.isArray(x));
   alert('y is an array: ' + $.isArray(y));
   alert('z is an array: ' + $.isArray(z));

In the above jQuery code, I have declared 3 variables x,y and z. And out of 3, x and y variables are array where z is not. When you run this then you will see that for variable x and y it returns true and for z it returns false.

Check this article to find find index of element in array using jQuery.

See live Demo and Code.

Feel free to contact me for any help related to jQuery. I will gladly help you.

Responsive Menu
Add more content here...