Javascript is_array()
Click here to view the is_array() demo (URL: /javascript-is-array-demo.html)
Here's my version of PHP's is_array() function, made for JavaScript. Feel free
to copy-and-paste it:
The Function
Using is_array
Using the JavaScript is_array() is the same as in PHP. Just pass it a variable,
and it will return true if the variable is an array:
Code Explanation
Let's break down the two pieces of this function.
The typeof() function accepts any input and will return that input's type. So,
if it says that the type of our function input is 'object', that means our input
is an object. An array is a type of object in JavaScript.
Here is a list of some common JavaScript variable types:
string
Boolean
number
function
object
undefined
Next, we just make sure that the input is the correct type of object. We can
check if it is an instance of the Array object by using instanceof.