Computer >> Computer tutorials >  >> Programming >> Javascript

How can I check whether a variable is defined in JavaScript?


To check a variable is ‘undefined’, you need to check using the following. If the result is “false”, it means the variable is not defined. Here, the variable results to “True” i.e. defined −

Example

Live Demo

<html>
   <body>
      <script>
         var rank = 1;
         if(rank){
            document.write("True");
         } else{
            document.write("False");
         }
      </script>
   </body>
</html>