JavaScript isArray() Function

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.

The isArray() Function

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.

Example 1

var social = ["facebook","Whatsapp","Instagram"];
console.log(Array.isArray(social));

Explanation

  • We first declare one array in variable str.
  • Then we use isArray() function for checking the input.
  • And we console the output of isArray() function.
    [We can check console value by using the inspect element and then open the console tab.]

Note

  • The isArray() function returns true if the passed value is array.
  • The isArray() function returns false if the passed value is not an array.

Following are some examples to understand the isArray() function in details:

Example 2

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

  • First, we create an empty array and put it in variable ‘social’
  • Next, we select the <p> tag using its id test.
  • Then we check the variable social using isArray() function and we return the result into innerHTML of <p> tag.

Example 3

<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

  • First, we create an array and put it in variable ‘social’.
  • Next, we select the <p> tag using its id test.
  • Then we check the variable social using isArray() function and we return the result into innerHTML of <p> tag.

Example 4

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

  • First we put an integer in variable m.
  • Next we put a string in variable n.
  • Next we select the <p> tag using its id test.
  • Then we check the variable social using isArray() function and we return the result into innerHTML of <p> tag.
  • Then after we select the <p> tag using its id test2.
  • Finally we check the variable social using isArray() function and we return the result into innerHTML of <p> tag.

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.

Example 5

<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

  • First, we create an array and put it in variable ‘social’.
  • Next we select the <p> tag using its id test.
  • Then we check the variable social using isArray() function
  • Then after we check the result using if condition and print the result in innerHTML of <p> tag.

Read Also: JavaScript Array Methods

Conclusion

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.

Leave a Reply

Your email address will not be published. Required fields are marked *