314

In Firebug, the DOM tab shows a list of all your public variables and objects. In Chrome's console you have to type the name of the public variable or object you want to explore.

Is there a way - or at least a command - for Chrome's console to display a list all the public variables and objects? It will save a lot of typing.

Charles
  • 50,943
  • 13
  • 104
  • 142
GRboss
  • 6,239
  • 5
  • 21
  • 20

16 Answers16

409

Is this the kind of output you're looking for?

for(var b in window) { 
  if(window.hasOwnProperty(b)) console.log(b); 
}

This will list everything available on the window object (all the functions and variables, e.g., $ and jQuery on this page, etc.). Though, this is quite a list; not sure how helpful it is...

Otherwise just do window and start going down its tree:

window

This will give you DOMWindow, an expandable/explorable object.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • DOMWindow doesn't have a `hasOwnProperty` method. – ntownsend May 29 '10 at 12:29
  • 4
    @ntownsend -My console disagrees with you :) [It's a property of `object`](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty), why wouldn't it have it? – Nick Craver May 29 '10 at 12:36
  • @Nick Craver - Withdrawn. When I posted the above comment, I did a test in my console that showed `window` had no `hasOwnProperty` method. Indeed, at that time, it didn't. I've just checked again, after reading your response, and my console now agrees that `window` has such a method. Mysterious. – ntownsend Jun 04 '10 at 06:11
  • 9
    "why wouldn't it have it?" The `[[Prototype]]` internal property of the global object is *implementation dependent*, in almost all major implementations -V8, Spidermonkey, Rhino, etc-, the global object inherits at some point from `Object.prototype`, but for example in other implementations -JScript, BESEN, DMDScript, etc...- it doesn't, so `window.hasOwnProperty` doesn't exist, to test it we can: `Object.prototype.isPrototypeOf(window);` – Christian C. Salvadó Oct 01 '10 at 22:38
  • 12
    @CMS - Yes that's true...but the question is specifically about Chrome, so the implementation is known. – Nick Craver Oct 01 '10 at 22:40
  • @CMS - Fair point, this is sure to come up in google for lots of other searches, +1 from me. – Nick Craver Oct 01 '10 at 22:44
  • 6
    Or you could just type this; – Edward J Beckett Dec 17 '14 at 03:46
  • This just saved me after about a day's worth of head banging....some global variable was defined in my user profile, and it kept stopping my js scripts. Was very frustrating – jdlam Aug 11 '16 at 20:07
  • 3
    I wanted to see the value of the variable as well so i used: `for(var b in window) { if(window.hasOwnProperty(b)) console.log(b+" = "+window[b]); }` – northern-bradley Nov 07 '16 at 06:21
82

When script execution is halted (e.g., on a breakpoint) you can simply view all globals in the right pane of the Developer Tools window:

chrome-globals

Community
  • 1
  • 1
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
77

Open the console and then enter:

  • keys(window) to see variables
  • dir(window) to see objects
danday74
  • 52,471
  • 49
  • 232
  • 283
arkadiusm
  • 771
  • 5
  • 2
49

If you want to exclude all the standard properties of the window object and view application-specific globals, this will print them to the Chrome console:

{

    const standardGlobals = new Set(["window", "self", "document", "name", "location", "customElements", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "top", "opener", "parent", "frameElement", "navigator", "origin", "external", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "visualViewport", "screenX", "screenY", "outerWidth", "outerHeight", "devicePixelRatio", "clientInformation", "screenLeft", "screenTop", "defaultStatus", "defaultstatus", "styleMedia", "onsearch", "isSecureContext", "performance", "onappinstalled", "onbeforeinstallprompt", "crypto", "indexedDB", "webkitStorageInfo", "sessionStorage", "localStorage", "onabort", "onblur", "oncancel", "oncanplay", "oncanplaythrough", "onchange", "onclick", "onclose", "oncontextmenu", "oncuechange", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onformdata", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onvolumechange", "onwaiting", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkittransitionend", "onwheel", "onauxclick", "ongotpointercapture", "onlostpointercapture", "onpointerdown", "onpointermove", "onpointerup", "onpointercancel", "onpointerover", "onpointerout", "onpointerenter", "onpointerleave", "onselectstart", "onselectionchange", "onanimationend", "onanimationiteration", "onanimationstart", "ontransitionrun", "ontransitionstart", "ontransitionend", "ontransitioncancel", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onmessageerror", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onrejectionhandled", "onstorage", "onunhandledrejection", "onunload", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "cancelIdleCallback", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "createImageBitmap", "fetch", "find", "focus", "getComputedStyle", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "postMessage", "print", "prompt", "queueMicrotask", "releaseEvents", "requestAnimationFrame", "requestIdleCallback", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitRequestAnimationFrame", "chrome", "caches", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "originAgentCluster", "cookieStore", "showDirectoryPicker", "showOpenFilePicker", "showSaveFilePicker", "speechSynthesis", "onpointerrawupdate", "trustedTypes", "crossOriginIsolated", "openDatabase", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]);

    for (const key of Object.keys(window)) {
        if (!standardGlobals.has(key)) {
            console.log(key)
        }
    }
}

The script works well as a bookmarklet. To use the script as a bookmarklet, create a new bookmark and replace the URL with the following:

javascript:(() => {
    const standardGlobals = new Set(["window", "self", "document", "name", "location", "customElements", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "top", "opener", "parent", "frameElement", "navigator", "origin", "external", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "visualViewport", "screenX", "screenY", "outerWidth", "outerHeight", "devicePixelRatio", "clientInformation", "screenLeft", "screenTop", "defaultStatus", "defaultstatus", "styleMedia", "onsearch", "isSecureContext", "performance", "onappinstalled", "onbeforeinstallprompt", "crypto", "indexedDB", "webkitStorageInfo", "sessionStorage", "localStorage", "onabort", "onblur", "oncancel", "oncanplay", "oncanplaythrough", "onchange", "onclick", "onclose", "oncontextmenu", "oncuechange", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onformdata", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onvolumechange", "onwaiting", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkittransitionend", "onwheel", "onauxclick", "ongotpointercapture", "onlostpointercapture", "onpointerdown", "onpointermove", "onpointerup", "onpointercancel", "onpointerover", "onpointerout", "onpointerenter", "onpointerleave", "onselectstart", "onselectionchange", "onanimationend", "onanimationiteration", "onanimationstart", "ontransitionrun", "ontransitionstart", "ontransitionend", "ontransitioncancel", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onmessageerror", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onrejectionhandled", "onstorage", "onunhandledrejection", "onunload", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "cancelIdleCallback", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "createImageBitmap", "fetch", "find", "focus", "getComputedStyle", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "postMessage", "print", "prompt", "queueMicrotask", "releaseEvents", "requestAnimationFrame", "requestIdleCallback", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitRequestAnimationFrame", "chrome", "caches", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "originAgentCluster", "cookieStore", "showDirectoryPicker", "showOpenFilePicker", "showSaveFilePicker", "speechSynthesis", "onpointerrawupdate", "trustedTypes", "crossOriginIsolated", "openDatabase", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]);
    for (const key of Object.keys(window)) {
        if (!standardGlobals.has(key)) {
            console.log(key)
        }
    }
})()
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
  • 2
    This is a list of current Chrome and Firefox default globals: http://pastebin.com/wNj3kfg0 – Henry Ruhs Oct 31 '14 at 11:49
  • Unfortunately an outdated list, but still very helpful! If I had more time I'd contribute a way to get the current standardGlobals in a blank window in an easy way (remind me in a few hours and I should have some time for that, in case anyone is interested and can't do it themselves). – Luc Feb 12 '21 at 13:05
  • 1
    Updated for 2021, but some of the other answers seem evergreen. – Max Heiber Feb 12 '21 at 14:21
46

The window object contains all the public variables, so you can type it in the console and then expand to view all variables/attributes/functions.

chrome-show-all-variables-expand-window-object

Daniel Sokolowski
  • 11,982
  • 4
  • 69
  • 55
Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
  • 4
    Nice! By far the easiest way since you can recursively expand variables. – qwertzguy May 09 '15 at 02:15
  • This is a good method to see `document` as the browser does by elements and `document.` document(dot) for document properties. And `window.document` clarifies a lot of confusion about [window vs document](https://stackoverflow.com/questions/9895202/what-is-the-difference-between-window-screen-and-document-in-javascript) – Stephan Luis Feb 27 '21 at 16:41
14

Type the following statement in the javascript console:

debugger

Now you can inspect the global scope using the normal debug tools.

To be fair, you'll get everything in the window scope, including browser built-ins, so it might be sort of a needle-in-a-haystack experience. :/

tangentstorm
  • 7,183
  • 2
  • 29
  • 38
10

David Walsh has a nice solution for this. Here is my take on this, combining his solution with what has been discovered on this thread as well.

https://davidwalsh.name/global-variables-javascript

x = {};
var iframe = document.createElement('iframe');
iframe.onload = function() {
    var standardGlobals = Object.keys(iframe.contentWindow);
    for(var b in window) { 
      const prop = window[b];
      if(window.hasOwnProperty(b) && prop && !prop.toString().includes('native code') && !standardGlobals.includes(b)) {
        x[b] = prop;
      }
    }
    console.log(x)
};
iframe.src = 'about:blank';
document.body.appendChild(iframe);

x now has only the globals.

Avindra Goolcharan
  • 4,032
  • 3
  • 41
  • 39
  • 1
    `prop.toString` seems to not exist everywhere, so the condition could be more defensive `if(window.hasOwnProperty(b) && prop && (prop.toString && !prop.toString().includes('native code')) && !standardGlobals.includes(b))` – yves amsellem Jan 24 '19 at 10:04
9

To view any variable in chrome, go to "Sources", and then "Watch" and add it. If you add the "window" variable here then you can expand it and explore.

TigerBear
  • 2,479
  • 1
  • 21
  • 24
7

List the variable and their values

for(var b in window) { if(window.hasOwnProperty(b)) console.log(b+" = "+window[b]); }

enter image description here

Display the value of a particular variable object

console.log(JSON.stringify(content_of_some_variable_object))

enter image description here

Sources: comment from @northern-bradley and answer from @nick-craver

intika
  • 8,448
  • 5
  • 36
  • 55
7

I ended using this for debugging purposes:

for (aProperty in window) {
    try{
        console.log(aProperty +':'+JSON.stringify(window[aProperty]));
    }catch{}
}

try is used to avoid TypeError: Converting circular structure to JSON
Then Save as... console output to a file and manipulate further.

Hebe
  • 661
  • 1
  • 7
  • 13
6

Updated method from same article Avindra mentioned — injects iframe and compare its contentWindow properties to global window properties.

(function() {
  var iframe = document.createElement('iframe');
  iframe.onload = function() {
    var iframeKeys = Object.keys(iframe.contentWindow);
    Object.keys(window).forEach(function(key) {
      if(!(iframeKeys.indexOf(key) > -1)) {
        console.log(key);
      }
    });
  };
  iframe.src = 'about:blank';
  document.body.appendChild(iframe);
})();
James Morgan
  • 177
  • 2
  • 6
  • This doesn't seem to work anymore. No iframe is created - does it have to be manually triggered? – jzadra Nov 10 '22 at 23:10
5

Type: this in the console,

to get the window object I think(?), I think it's basically the same as typing window in the console.

It works at least in Firefox & chrome.

Sebastian Norr
  • 7,686
  • 2
  • 12
  • 17
4

You may want to try this Firebug lite extension for Chrome.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
1

As all "public variables" are in fact properties of the window object (of the window/tab you are looking at), you can just inspect the "window" object instead. If you have multiple frames, you will have to select the correct window object (like in Firebug) anyway.

mihi
  • 6,507
  • 1
  • 38
  • 48
1

Try this simple command:

console.log(window)
-1

enter image description here

GRboss
  • 6,239
  • 5
  • 21
  • 20