JavaScript | Array.isArray()
Array.isArray() function determines whether the value passed to this function is an array or not. This function returns true if the argument passed is array else it returns false. The syntax of this function is as follows:
Array.isArray(obj)
Argument
Here obj is any valid object in JavaScript like map, list, array, string, etc.
Return value
This function returns Boolean value true if the argument passed is array otherwise it returns false
Examples for the above function are as follows:
Example 1:
Input: print(Array.isArray(['Day','Night','Evening'])); Output: true
Since the argument passed to the function isArray() is an array therefore this function returns true as the answer.
Example 2:
Input: print(Array.isArray({foo: 123})); Output: false
Since the argument passed to the function isArray() is a map therefore this function returns false as the answer.
Example 3:
Input: print(Array.isArray('foobar')); Output: false
Since the argument passed to the function isArray() is a string therefore this function returns false as the answer.
Codes for the above function are as follows:
Program 1:
<script> // JavaScript code for isArray() function function func() { document.write(Array.isArray([ 'Day' , 'Night' , 'Evening' ])); } func(); </script> |
Output:
true
Program 2:
<script> // JavaScript code for isArray() function function func() { document.write(Array.isArray({foo: 123})); } func(); </script> |
Output:
false
Program 3:
<script> // JavaScript code for isArray() function function func() { document.write(Array.isArray( 'foobar' )); } func(); </script> |
Output:
false
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.