How to check if a variable exists in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To check if a variable exists in JavaScript, you need to check it against null as in the following code. Here, we’re checking the existence of variable myVar −

<html>
   <body>
      <script>
         var myVar = 20;
         if(myVar !== undefined && myVar !== null) {
            document.write("Variable exists");
         }
      </script>
   </body>
</html>
raja
Published on 16-Jan-2018 11:15:39
Advertisements