JavaScript

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.

Yash Panchal

Computer science student, Working as Web Developer and part-time Blogger. Highly interested in AI & ML.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

4 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago