title: angular.isDefined vs typeof
body: <p>This question is specific to angular.isDefined() being named isDefined. We don't expect it to find a way to return false if the variable passed to it is not defined rather than throwing an uncaught error. I am looking for an angular equivalent of typeof operator to detect the not <strong>Defined</strong> variables.In the below example <code>x</code> is not <strong>Defined</strong> and <code>typeof x</code> doesn't throw any <code>Uncaught ReferenceError: x is not defined</code> error.where in if i had used <code>angular.isDefined</code> i would have received a <code>not defined error</code>.  </p>
  
  <pre><code> if(typeof x != 'undefined')
       alert("defined");
   else 
        alert("undefined");    
  </code></pre>
  
  <p><strong>angular.isDefined(x)</strong>  throws <code>Uncaught ReferenceError: x is not defined</code> if x is not <strong>Defined</strong> .</p>
  
  <pre><code>  if(angular.isDefined(x)) // throws error because x is not defined
       alert("defined");
   else 
        alert("undefined"); 
  </code></pre>
  
  <hr>
  
  <pre><code>     var x;
        if(angular.isDefined(x)) // does not throw error as x is defined though not **initialiased** 
           alert("defined");
        else 
           alert("undefined"); 
  </code></pre>
  
  <p>Is there an angular alternative to <code>typeof x !='undefined'</code>.Let me explain how am i ending up with this scenario.I have <code>createContext()</code> function in a js file which i am not allowed to modify.and this function defines <code>context="something useful"</code>and i have two controllers  <code>commonController</code> and <code>contextController</code>.<code>commonController</code> is executed first and registers for an custom eventhandler which needs access to <code>context</code> .then <code>commonController</code> executes which calls <code>createContext()</code> .sometimes depending on  the execution time even before the <code>createContext()</code> is called.the eventHadler is triggered and would try to access the variable which is not yet defined. I can not call <code>createContext()</code> anywhere else other than in <code>contextController</code>.</p>