title: Function to check if variable is not undefined and not null
body: <p>I'm looking for a way to do the following:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var test = 'I exist!';
  var testNull = null;
  var testUndefined;
  
  function checkVar(varToCheck) {
    return typeof varToCheck != 'undefined' &amp;&amp; varToCheck != null;
  }
  
  console.log(checkVar(test)); // Logs true
  console.log(checkVar(testNull)) // Logs false
  console.log(checkVar(testUndefined)) // Logs false
  console.log(checkVar(undefinedVar)) // Logs false</code></pre>
  </div>
  </div>
  </p>
  
  <p>When it tries to execute the last line, instead of <code>false</code>, this throws an error: <code>Uncaught ReferenceError: undefinedVar is not defined</code>.</p>
  
  <p>I know it can be done with:</p>
  
  <pre><code>if (typeof varToCheck != 'undefined' &amp;&amp; varToCheck != null) {
      alert('something happens!')
  }
  </code></pre>
  
  <p>but it's becoming annoyingly repetitive to use long conditions in my project, once I have a lot of variables to check. Any ideas?</p>