angular.isUndefined


angular.isUndefined: This function is used to check undefined variable in AngularJs. The function angular.isUndefined will return true if passed variable is undefined else it will return false. Here in this tutorial we are going to explain how you can use this function to determine a undefined variable in AngularJs. You can use our online editor to edit and run the code online.


angular.isUndefined – AngularJs check undefined value | Example

Sytax for angular.isUndefined is as below-

Syntax –

angular.isUndefined Syntax

angular.isUndefined(value);

The above function checks whether the passed variable is undefined or not and on the basis of that it returns true or false.

Arguments

  • value : This is reference to check.

Returns

  • boolean : Returns true if reference is not defined ie. undefined else it will return false.

Now let us create a simple example to understand the above function.

Example

AngularJs check undefined value | Variable Example

<!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) {
  var b = 100;
  var a;
  $scope.result1 = '';
  $scope.result2 = '';
  $scope.chackVars = function() {
    if(angular.isUndefined(a)){
		$scope.result1 = 'Undefined';
	}else{
		$scope.result1 = 'Defined';
  }
  if(angular.isUndefined(b)){
		$scope.result2 = 'Undefined';
	}else{
		$scope.result2 = 'Defined';
  }
  }
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
    <button ng-click ="chackVars()" >Check Vars</button>
	<br>
	Result for a = {{result1}}<br>
	Result for b = {{result2}}
</div> 
</div>
</body>  
</html>                                                                                                                                                                           

Try it »

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

AngularJs check undefined variable


Advertisements

Add Comment

📖 Read More