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>

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements