Search notes:

JavaScript: undefined

typeof(undefined) returns "undefined".
By comparing a variable to undefined (=== or == operator), it is possible to check if a value was assigned to the variable. var === undefined returns true if so and false otherwise.
Of course, it's possible to assign undefined to a variable in which case the variable becomes undefined again.
See also Comparing undefined to other values.
Interestingly, undefined+undefined evaluates to NaN.
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>undefined</title>

  <script type="text/javascript">

    function main() {

      var out = document.getElementById('out');

      out.innerHTML  = (  typeof(undefined)     ) + "<br>"; //  undefined

      out.innerHTML += (  undefined + 'Foo'     ) + "<br>"; //  undefinedFoo
      out.innerHTML += (  undefined + undefined ) + "<br>"; //  NaN

      var unassignedVariable;
      var assignedVariable    = 42;

      if (unassignedVariable === undefined) { out.innerHTML += "unassignedVariable === undefined<br>"; } else { out.innerHTML += "unassignedVariable !== undefined<br>"; }
      if (unassignedVariable ==  undefined) { out.innerHTML += "unassignedVariable ==  undefined<br>"; } else { out.innerHTML += "unassignedVariable != undefined<br>";  }

      if (assignedVariable   === undefined) { out.innerHTML += "assignedVariable === undefined<br>"  ; } else { out.innerHTML += "assignedVariable !== undefined<br>";   }
      if (assignedVariable   ==  undefined) { out.innerHTML += "assignedVariable ==  undefined<br>"  ; } else { out.innerHTML += "assignedVariable !=  undefined<br>";   }

      assignedVariable = undefined;
      if (assignedVariable   === undefined) { out.innerHTML += "assignedVariable === undefined<br>"  ; } else { out.innerHTML += "assignedVariable !== undefined<br>";   }
      if (assignedVariable   ==  undefined) { out.innerHTML += "assignedVariable ==  undefined<br>"  ; } else { out.innerHTML += "assignedVariable !=  undefined<br>";   }

    }

  </script>

</head>
<body onload='main()';>
  <div id='out'>
  </div>
</body>
</html>
Github repository about-javascript, path: /objects/Global-Object/value-properties/undefined.html

See also

Global object: value properties

Index