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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Ali
Published on 18-Jan-2018 13:22:19
Advertisements