AngularJS – angular.isArray() Function


The angular.isArray() function in AngularJS basically checks if a reference is an Array or not. This method will return True if the reference passed inside the function is an Array type, else it will return False.

Syntax

angular.isArray(value)

Example − Check if the reference is an Array or not

Create a file "isArray.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isArray()</title>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
   </head>
   <body ng-app="app" style="text-align:center">
      <h1 style="color:green">
         Welcome to Tutorials Point
      </h1>
      <h2>AngularJS | angular.isArray()</h2>
      <div ng-controller="example">
         <b>Value: {{value}}</b>
         <br><br>
         {{isArray}}
         <br><br>
         <b>Value: {{value2}}</b>
         <br><br>
         {{isArray1}}
         <br><br>
         <b>Value: {{value3}}</b>
         <br><br>
         {{isArray2}}
         <br><br>
         <b>Value: {{value4}}</b>
         <br><br>
         {{isArray3}}
      </div>
      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("app", []);
         app.controller('example',['$scope', function ($scope)
         {
         // Defining the keys & values
         $scope.value = [];
         $scope.value2 = null;
         $scope.value3 = [{id:'1'},{id:'2'},{id:'3'},{id:'4'},{id:'5'}];
         $scope.value4 = [1,2,3,4,5];


         $scope.isArray = angular.isArray($scope.value) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";

         $scope.isArray1 = angular.isArray($scope.value2) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";

         $scope.isArray2 = angular.isArray($scope.value3) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";

         $scope.isArray3 = angular.isArray($scope.value4) == true
            ? "$scope.array is an Array."
            : "$scope.array is not an Array.";
        }]);
      </script>
   </body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

Updated on: 06-Oct-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements