To check object is an array in javascript; In this tutorial, you will learn how to check object is array in javascript and array object is empty or not.
Sometimes, you need to check array objects; So this tutorial will show you multiple examples of how to check array objects in the array. And it has been empty or not in javascript
How to Check Object is Array In JavaScript
The JavaScript array.isArray() function determines whether the value given or object to this function is an array or not. If this argument is correct then this method is return true, otherwise return false.
Note that, this is the built-in javascript array method.
isArray() Syntax
Array.isArray(obj)
This method returns the boolean value. If the passed argument is array this method returned true, otherwise return false.
Example 1
var arr = [5, 3, 10, 1, 6, 12] document.write("Check if passed value array or not"); document.write("<br><br>"); document.write("Retrun value = " + Array.isArray(arr));
Output /////
Check if passed value array or not
Retrun value = true
We have passed an array in the IsArray () function. Therefore Returning the true.
Example 2
var arr = 'foobar'; document.write("Check if passed value array or not"); document.write("<br><br>"); document.write("Retrun value = " + Array.isArray(arr));
Output ///
Check if passed value array or not
Retrun value = false
We have passed the string value in the isArray () function. Therefore this function is returning the false .
Example 3
var arr = [ ['php', 'python', 'perl'], ['xampp', 'mysql', 'sql'], ['jquery', 'javaScript', 'angular', 'react'] ]; document.write("Check if passed value array or not"); document.write("<br><br>"); document.write("Retrun value = " + Array.isArray(arr));
Output///
Check if passed value array or not
Retrun value = true