34

For simple javascript debugging I'll use alerts to show variable values and the like. Is there a way to get the current call stack in javascript to be able to display it in an alert?

Thanks.

2
  • Is this for debugging or production? Im not sure there is any standard way to do this across all browsers, but there might be some useful features specific to only some browsers... Commented Jan 13, 2010 at 21:20
  • This would just be for debugging purposes. Commented Jan 13, 2010 at 21:25

7 Answers 7

47

Quick and dirty in Gecko-based browsers:

new Error().stack

You can also manually trawl some of the stack using Function.prototype.caller:

var thisFunction = arguments.callee;
var caller = thisFunction.caller;
var callerCaller = caller.caller;
// ...and eventually, assuming no recursion:
var bottomCaller = ...;
assert(bottomCaller.caller === null);

One (possibly large) caveat of the .caller trick is that it doesn't handle recursion -- .caller looks from the top of the stack downward to find the first instance of the function in the stack and then returns its immediate caller, so without being careful you can loop infinitely looking up callers.

Another caveat to caller is that, going forward, if any of your code uses ECMAScript 5's strict mode, the caller property of strict mode functions (or of functions which have themselves been called from strict mode functions) is a so-called "poison pill" which throws a TypeError when accessed. The caller property of "bound" functions (those created by ES5's Function.prototype.bind method) is also a poison pill. These restrictions break the generic stack-walking algorithm, although one could imagine use-specific ways to work around this (entry and exit annotating functions, perhaps).

Do note that stack-walking like this isn't a great idea in production code (as a quick hack for debugging it's fine, tho); at the moment walking up the stack as in the latter example is somewhat expensive in Mozilla's JS engine, and it'll probably throw you out of machine code and back into interpreted code. Also, the stack-walk is O(n2), which might matter if you tend to have complex, deep stacks.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this answer still relevant today?
Stack-walking isn't quite as expensive as it used to be, but it's still going to hurt perf to a degree. Some of the finer details of how the poison-pill properties function have changed in recent ECMAScript editions, but the overall thrust is mostly the same. Beyond those two bits, the comment remains fundamentally relevant, I think.
42

You can use console.trace()

It doesn't display an alert() but print the stacktrace on the debugger console.

2 Comments

exactly what I was looking for. This is especially useful if you want to dump the call stack while the page is loading.
You can use it in Chrome now as well
6

Use debugger like Firebug for this if you are in Firefox. Chrome and Opera have build-in debugger. And there are Developers Tools for Internet Explorer.

3 Comments

Thanks, I am aware of those options, but I was wondering if there was a way to do it without using a debugger.
IMO easiest and fastest way to debug javascript is to use debugger.
Using a debugger is no problem and usually convenient, I was just looking for a way to look up the call stack in the right situation.
3

The best way to debug Javascript is to use Firebug, which includes a full Javascript debugger.

If you're debugging in IE, you can use Visual Web Developer Express (or any other edition of Visual Studio).
If you're debugging IE8, you can use its built-in developer tools, which include a debugger.

It is possible to get the call stack in Javascript; see here.

Comments

3

Have you looked at firebug - and a breakpoint. If it's just for debugging, then this might suffice.

Also - you can have a look Here

Comments

1

This will give you all call stack working good for me.

var y = 'arguments.callee.caller';
    while (eval(y) != undefined) {
        stak += eval(y + '.toString()');
        y = y + '.caller';
    }
    alert(stak);

Comments

0

For nodejs debugging, in Visual Studio Code, as of v.1.14.2, it's View->Debug (Ctrl+Shift+D)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.