Sometimes we have to make sure that a variable is an Array type object. The isArray() is basically a JavaScript function to check the object is of array type.
Index
The isArray() is a predefined method of Array class in javascript. It checks wheater the input value is the Array type or not.
Syntax
Array.isArray(object)
Explanation
An object parameter is a required field of function.
var social = ["facebook","Whatsapp","Instagram"]; console.log(Array.isArray(social));
Explanation
Note
Following are some examples to understand the isArray() function in details:
Suppose we create an empty array, and then we check its type using isArray() function.
<html> <body> <p id="test"></p> <script> var social = []; var x = document.getElementById("test"); x.innerHTML = Array.isArray(social); </script> </body> </html>
Output
True
Explanation
<html> <body> <p id="test"></p> <script> var social = ["facebook","whatsapp","instagram"]; var x = document.getElementById("test"); x.innerHTML = Array.isArray(social); </script> </body> </html>
Output
True
Explanation
Suppose we insert number or string as an input of isArray function then it will show output as follows:
<html> <body> <p id="test"></p> <p id="test2"></p> <script> var m = 5; var n = "Instagram"; var x = document.getElementById("test"); x.innerHTML = Array.isArray(m); var y = document.getElementById("test2"); y.innerHTML = Array.isArray(n); </script> </body> </html>
Output
false
false
Explanation
Above are the simple examples of the isArray() function. Now, we will use the result of the isArray() function for checking conditions, and we will provide output according to the result.
<html> <body> <p id="test"></p> <script> var social = ["facebook","whatsapp","instagram"]; var x = document.getElementById("test"); if(Array.isArray(social) == true){ x.innerHTML = "I am array"; }else{ x.innerHTML = "I am not an array"; } </script> </body> </html>
Explanation
Read Also: JavaScript Array Methods
This is all about the isArray() function. I hope now you have a complete understanding of JavaScript isArray() method. We can use it when we require to check whether the result is an array or not.
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…