To check object is an array in JavaScript; Through this tutorial, i am going to show you how to check object is array in JavaScript.
How to Check If Object is Array in JavaScript
Using JavaScript array.isArray() method, you can check whether the value given or object or variable is an array or not.
Note that:- If given argument is correct then this method is return true, otherwise return false.
Syntax of JavaScript isArray() Method
See the following syntax of isArray() method; as shown below:
Array.isArray(obj)
This method returns the boolean value. If the passed argument is array this method returned true, otherwise return false.
Example 1 – To check if variable or object is array javaScript
Here, i will take simple example for check if variable or object is array javaScript; as shown below:
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
Example 2 – How to check if variable is array or object javaScript
Here, i will take second example using isArray() method to check if variable is array or object javascript; as shown below:
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