Open In App

Underscore.js _.isArray() Function

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke, etc even without using any built-in objects. The _.isArray() function is used to find whether the passed argument is an array or not. An array is a set of variables, constants, and special symbols. True will be returned if the object is an array else false will be returned. The array can have different names and can be of any size even zero. 

Syntax:

_.isArray(object)

Parameters: It takes only one argument, i.e., the object that needs to be checked. 

Return value: It returns ‘true’ if the argument passed is an array else it returns ‘false’. 

Passing an array of 3 numbers to the _.isArray() function: The _.isArray() function takes the element passed and checks whether it is an array or not. Since the passed argument is a set of 3 numbers – 1, 2, 3. Therefore, it is an array. And Hence, the final output will be true. 

Example: 

html




<html>
 
<head>
    <script src=
  </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isArray([1, 2, 3]));
    </script>
</body>
 
</html>


Output:

 

Passing an array of characters to the _.isArray() function: The argument passed to the _.isArray() function will be checked that is it an array or not. Since the argument is containing only one character, “a” but is inside [] brackets so it is an array. And hence the answer is true. 

Example: 

html




<html>
 
<head>
    <script src=
  </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isArray(["a"]));
    </script>
</body>
 
</html>


Output:

 

Passing an empty array to _.isArray() function: The _.isArray() function takes the element which here is [] and then checks whether it is an array or not. Since there is no element inside the [] brackets so, it is empty. But since [] brackets are present so it is an array. Since the empty array is also an array so the output is true. 

Example: 

html




<html>
 
<head>
    <script src=
  </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isArray([]));
    </script>
</body>
 
</html>


Output:

 

Passing a number to the _.isArray() function: If we pass any random number to the _.isArray() function then it will check whether the argument is an array or not. Since a number is passed to the _.isArray() function therefore the output will be false. 

Example: 

html




<html>
 
<head>
    <script src=
  </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.isArray(1));
    </script>
</body>
 
</html>


Output:

 

NOTE: 
These commands will not work in Google Console or in Firefox as these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them. The links are as follows: 

HTML



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads