AngularJS – isUndefined() method


The isUndefined() method in AngularJS basically checks if a reference is defined or not. This method will return True if the reference passed inside the function is not defined or undefined, else it will return False.

Syntax

angular.isUndefined(value)

Example − Check if the reference isUndefined or not

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

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isUndefined()</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.isUndefined()</h2>

      <div ng-controller="example">
         <b>Name: {{name}}</b>
         <br><br>
         {{isUndefined}}
         <br><br>
         <b>Name: {{name2}}</b>
         <br><br>
         {{isUndefined1}}
      </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.name = "SIMPLY LEARNING";
            $scope.name2;

            $scope.isUndefined = angular.isUndefined($scope.name) == true
               ? "$scope.name is Undefined."
               : "$scope.name is Defined.";

            $scope.isUndefined1 = angular.isUndefined($scope.name2)== true
               ? "$scope.name2 is Undefined."
               : "$scope.name2 is Defined.";
         }]);
      </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.

In the code, $scope.name is defined, whereas $scope.name2 is not undefined. Hence, we got this output.

Updated on: 08-Oct-2021

359 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements