The isArray function serves the purpose of determining if the provided value is an array. If the function identifies that the supplied value is indeed an array, it yields True as the result. Conversely, if the value is not an array, it outputs False.
Syntax
Array.isArray(obj_value);
Parameter
obj_value: This represents the value of the object that is utilized to ascertain whether it qualifies as an array.
Return
It yields either a false or true value, contingent upon the evaluation conducted.
JavaScript Array isArray Method Example
Let’s examine the implementations provided below and assess whether the given value is an array.
Example1
This example demonstrates a fundamental application of the isArray method.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
document.write(Array.isArray(1,2,3,4)); //Testing the passed values.
</script>
</body>
</html>
Output:
It is evident that the values that were provided did not constitute an array. Therefore, the outcome is false.
Example2
In this illustration, we will provide an array as input and examine the resulting output.
<httml>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
var arr=new Array(1,2,34,4,5);
document.write(Array.isArray(arr)); //It will return true.
</script>
</body>
</html>
Output:
Therefore, the output indicates that the provided value is an array.
Example3
Let's examine the scenario where we provide 'null' as a value for an object.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
document.write(Array.isArray(null)); //It will return false.
</script>
</body>
</html>
Output:
Example4
Below is the code implementation in which the object value is conveyed as 'undefined'.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
document.write(Array.isArray(undefined)); //It will return false.
</script>
</body>
</html>
Output:
Example5
Assessing the value of the object and subsequently calling the appropriate functions.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
var arr=[10,20,40,50]; //An integer array.
function f1() {
document.write("arr is forming an array i.e., <br>" +arr);
}
function f2() {
document.write("arr does not form any array. <br>");
}
if(Array.isArray(arr))
f1();
else
f2();
</script>
</body>
</html>
Output:
The function f1 is called due to the argument provided being an array.
Example6
When we pass the object value as 'True'.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
document.write(Array.isArray(true));
</script>
</body>
</html>
Output:
It indicates that the value does not conform to the structure of an array.