See the global state of a script?

Is there any way to see the global state of a script? For example, all variables currently assigned and their contents. Or at the very least, something similar to var_dump in PHP. I am having a hard time debugging code.

You should be able to see the values of all the variables at any time from within the debugger. You just need to set a breakpoint at the appropriate spot to interrupt execution at the point you want to check what is defined.

As felgall says, use the debugger.
I saw this article recently that explains it quite well: http://swizec.com/blog/javascript-debugging-slightly-beyond-console-log/swizec/6633

The debugger bit is towards the end.

When you have the debugging console open (ctrl+shift+j in Chrome) and include the debugger keyword in your script, execution will pause there allowing you to examine the state of things at that point.

For example:

(function () {
    var foo = 'bar';
    var baz = foo.substr(0, 2) + 'z';
    debugger;
    // other stuff can be below here too
}());

Use Ctrl+Shift+J to open the debugging console, reload the page and it breaks in to the code, letting you examine things from there.

1 Like

Thanks everyone! Just what I was looking for.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.