JS isArray

The isArray() method returns true if an object is an array, otherwise false.

Array.isArray()

Array.isArray() is a static property of the JavaScript Array object.

You can only use it as Array.isArray().

Using x.isArray(), where x is an array will return undefined.

Syntax

Array.isArray(obj)

Parameters

Parameter Description
obj Required.
An object (or any data type) to be tested.

Return Value

Type Description
A boolean true if the object is an array, otherwise false.
<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Sanjay", "Aman", "Rehman"];
    document.write(a + "<br><br>");
    var b = Array.isArray(a);
    document.write(b);
  </script>
</head>
<body>

</body>
</html>

isArray() with if condition

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Sanjay", "Aman", "Rehman"];
    if(Array.isArray(a)){
        document.write("This is an Array"); 
    }else{
        document.write("This is not an Array");  
    }
  </script>
</head>
<body>
</body>
</html>