Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to check for “undefined” variable in JavaScript?

With JavaScript, I want to find whether a variable is ‘undefined’. The following is my code, but I am not sure about it and it checks both undefined and null:

function isEmpty(age) {
   return (age === undefined || age == null) ? true : false;
}


1 Answer
Samual Sam

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>

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.