Creating a sample array.
NB: THis method not supported in older browsers.
The function above always returns true if the argument is an array.
Or more precisely: it returns true if the object prototype contains the word "Array".
var fruits = ["Banana", "Orange", "Apple", "Mango"];
Method 1:
Array.isArray(fruits); // returns true
NB: THis method not supported in older browsers.
Method 2:
To overcome the method 1, Cretae an isArray() functionfunction isArray(x)
{
return x.constructor.toString().indexOf("Array") > -1;
}
{
return x.constructor.toString().indexOf("Array") > -1;
}
The function above always returns true if the argument is an array.
Or more precisely: it returns true if the object prototype contains the word "Array".
Method 3:
The instanceof operator returns true if an object is created by a given constructor:fruits instanceof Array // returns true
No comments:
Post a Comment