JavaScript check if is Undefined

To check if the variable is undefined, do this:

<script>
   if (typeof variable === "undefined") {
      // do something
   } 
</script>

Example:

<div id="name"></div>

<script>
   if (typeof username === "undefined") {
      document.getElementById('name').innerHTML = 'Name is undefined';
   } else {
      document.getElementById('name').innerHTML = name;
   }
</script>

Output: Name is undefined