AngularJs Check if Reference is Array


AngularJs Check if Reference is Array – You can use the function angular.isArray() to chack whether a variable is array or not in AngularJs. Here in this tutorial we are going to explain how you can use angular.isArray() in AngularJs. You can use our online editor to edit and run the code online.


AngularJs Check if Reference is Array | Check if Variable is Array | Example

Syntax

Syntax for angular.isArray() function is –

Angularjs Check if Variable is Array | Syntax :

angular.isArray(var);

Agruments

  • var – Value to be checked.

Return

This will return true if variable or string is array else false.

Example

Let us create a simple example to check if given string is array-

AngularJs Check if String is Array:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.html= '';
   var myArray = ['1','3'];
  $scope.checkArray = function() {
	if(angular.isArray(myArray)){
		alert("Is Array");		
	}
  }
   var myArray1 = '';
  $scope.checkArray1 = function() {
	if(angular.isArray(myArray1)){
		alert("Is Array");		
	}else{
		
		alert("Not An Array.");
	}
  }
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
	
    <button ng-click ="checkArray()">Example1 </button>
	<br>
	 <button ng-click ="checkArray1()">Example2</button>
	<br>
	
</div> 
</div>
</body>  
</html>

Try it »

If you run the above example it will produce output something like this –

AngularJs Check if Reference is Array


Advertisements

Add Comment

📖 Read More