title: How to find out where a function is called from in JavaScript?
body: <p><em>Is it possible to find out where a function is called from?</em> If yes, then
  how to detect, if a function is called from a <strong>global scope</strong>, from <strong>another function</strong>, or perhaps from a <strong>browser console</strong>?</p>
  
  <p>Take a look at the following example:</p>
  
  <pre><code>&lt;script&gt;
      function myFunc1() {
          // some code
          myFunc2(); // I was called from myFunc1()
      }
      function myFunc2() {
          var callerName = new String;
          callerName = arguments.callee.caller.name;
          // some code
          alert('I was called from ' + callerName + ' function');
      }
      myFunc2(); // I was called from global scope
  &lt;/script&gt;
  </code></pre>
  
  <p>I know that this line <code>callerName = arguments.callee.caller.name;</code>in the example above, would give me <em>caller function's name.</em> But I don't know <em>how to detect if a function was called from a global scope.</em> For instance if I change <code>myFunc2()</code> and add an <code>if else</code> statement to check if <code>arguments.callee.caller.name</code> returns an <code>undefined</code> value, knowing that this will happen, when a function is called from a global scope:</p>
  
  <pre><code>myFunc2() {
  var callerName = new String;
  callerName = arguments.callee.caller.name;
      if(callerName == undefined) {
          alert('I was called from global scope');
      } else {
          alert('I was called from ' + callerName + ' function');
      }
  }
  </code></pre>
  
  <p>However, this will not work if <code>myFunc2()</code> is called from a global scope and <code>callerName = arguments.callee.caller.name;</code> will cause JavaScript to throw the following error:</p>
  
  <pre><code>TypeError: 'null' is not an object (evaluating 'arguments.callee.caller.name')
  </code></pre>
  
  <p>So I am back to square one, and the question still remains:</p>
  
  <ul>
  <li>How to detect if a function is called from a <strong>global scope?</strong></li>
  <li>If it's called from a global scope, is it from a <strong>browser console?</strong></li>
  </ul>