| by Arround The Web | No comments

JavaScript Array isArray() Method

The Array.isArray() was released with the release of ECMAScript5 JavaScript. This method simply checks whether the argument passed to its arguments is an array or not. This article will explain this Array isArray() method by explaining its syntax and then showcasing some examples.

We will start by going over the syntax of the Array isArray() method.

Syntax
Observe the syntax of the Array isArray() below:

Array.isArray(Object)

In this syntax:

  • Array is the default JavaScript Array Object
  • Object is the argument, the one we want to determine as an array or not

Return Type

  • Boolean: Returns true if the object passed to this method was actually an array otherwise it would return false

Additional Information

Since this is a method of the default JavaScript Array Object, therefore it is also known as the static property of this Array Object.

Example 1: Passing an Array to Array.isArray() Method

To demonstrate the working of this method, first create an array of the same types of values with the help of the following line:

my_object = [1, 2, 3, 4, 5, 6, 7, 8, 9];

After that, pass this array to the Array.isArray() method and store the return value in a new variable named as the result:

result = Array.isArray(my_object);

After that, simply display the value inside the result variable on the terminal using the console log function:

console.log(result);

Execute the code, and observe the output to be:

The output shows that the object passed to this method was actually an array.

Example 2: Passing an Array With Different Data Type Values

To check whether this method works with an array containing values of different data types, create an array using the following line:

my_object = [1, 2, "Google", 4, true, 6, "7", 8.673, 9];

Pass this object into the Array.isArray() method and store the result in a result variable:

result = Array.isArray(my_object);

Afterwards, simply print the result from the result variable onto the terminal using the console log() function:

console.log(result);

Execute the code and observe the following output:

From the output, it is conclusive that the type of data stored inside the array doesn’t matter. It only checks whether the object is an array or not, which in this case was true.

Example 3: Passing a String Object in Array.isArray() Method

To demonstrate what happens when a non-array object is passed to the Array isArray() method, create a new string variable with the help of the following line:

string_var = "Hello World";

Pass this string value into the arguments of the Array.isArray() method and store the outcome in a new variable:

result_var = Array.isArray(string_var);

Print the value inside the result_var on the terminal using the console log() function:

console.log(result_var);

Execute the program and get the following output on the terminal:

It returns that the object passed into its argument was not an array.

Conclusion

The Array.isArray() method is pretty simple. It simply checks whether the object in its argument is an array or not and returns true or false to the caller. If an array is being passed, the values or even the data types of its values don’t matter. In this article, we have learned about the different outcomes of the Array.isArray() method with the help of different examples.

Share Button

Source: linuxhint.com

Leave a Reply