How to check for “undefined” variable in JavaScript?



On getting the result of the following, you can find whether a variable is ‘undefined’. If the result is “false”, it means the variable is undefined.

Here, the variable results in “True”

Example

Live Demo
<html>
   <body>
   
      <script>
         var res = 10;

         if(res) {
            document.write("True");
         } else {
            document.write("False");
         }
      </script>
      
    </body>
</html>
Updated on: 2019-07-30T22:30:21+05:30

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements