328

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.

16 Answers 16

422

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.

9
  • 4
    @ntownsend -My console disagrees with you :) It's a property of object, why wouldn't it have it? Commented May 29, 2010 at 12:36
  • 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); Commented Oct 1, 2010 at 22:38
  • 12
    @CMS - Yes that's true...but the question is specifically about Chrome, so the implementation is known. Commented Oct 1, 2010 at 22:40
  • 6
    Or you could just type this; Commented Dec 17, 2014 at 3:46
  • 4
    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]); } Commented Nov 7, 2016 at 6: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

2
  • 2
    can I spit out the vars from an execution context, like a break point show, without halting?
    – Mild Fuzz
    Commented Aug 6, 2013 at 15:37
  • 1
    @MildFuzz Then use Nick Craver's solution (the accepted one). Commented Oct 26, 2013 at 12:29
81

Open the console and then enter:

  • keys(window) to see variables
  • dir(window) to see objects
2
  • dir(Function("return this")()) makes it work in Web Workers too Commented Jan 8, 2015 at 15:31
  • 5
    FYI dir(window) doesn't work in Firefox (yes I know this thread was about Chrome), but key(window) does work in Firefox Commented Oct 29, 2015 at 16:14
54

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)
        }
    }
})()
3
  • 2
    This is a list of current Chrome and Firefox default globals: pastebin.com/wNj3kfg0
    – Henry Ruhs
    Commented Oct 31, 2014 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
    Commented Feb 12, 2021 at 13:05
  • 1
    Updated for 2021, but some of the other answers seem evergreen.
    – Max Heiber
    Commented Feb 12, 2021 at 14:21
48

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

2
  • 4
    Nice! By far the easiest way since you can recursively expand variables.
    – qwertzguy
    Commented May 9, 2015 at 2: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 Commented Feb 27, 2021 at 16:41
17

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. :/

0
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.

1
  • 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)) Commented Jan 24, 2019 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.

9

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.

1
  • chat gpt advies using console.dir(window) dunno if any better
    – Hebe
    Commented Jun 24, 2023 at 0:38
8

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

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);
})();

1
  • This doesn't seem to work anymore. No iframe is created - does it have to be manually triggered?
    – jzadra
    Commented Nov 10, 2022 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.

4

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

4
  • 3
    Though it does look nice, this solution sounds a bit of using a cannon to kill a mosquito to me. Commented May 29, 2010 at 15:25
  • Maybe. It's the only thing I found that shows objects/functions/etc. the way firebug in FF does (under the DOM-tab in the extension). It's a bit slow though.
    – KooiInc
    Commented May 29, 2010 at 16:31
  • 1
    As of May 17, your link is broken. Is this the same? getfirebug.com/releases/lite/chrome
    – Ian Hunter
    Commented May 17, 2012 at 23:09
  • @beanland 7: yep, fixed it in the answer, thnx for warning
    – KooiInc
    Commented May 18, 2012 at 6:16
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.

0
1

Try this simple command:

console.log(window)
1
  • 1
    It returns "undefined"
    – Shayan
    Commented Mar 17, 2019 at 19:21
-1

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.