Is there a way in Javascript to get a list or dump the contents of all global variables declared by Javascript/jQuery script on a page? I am particularly interested in arrays. If I can get the array names, it will be enough to me. Seeing its values is a bonus.
Share
This will give you an Array of all enumerable properties of the
windowobject, (which are global variables).For older browsers, include the compatibility patch from MDN.
To see its values, then clearly you’ll just want a typical enumerator, like
for-in.You should note that I mentioned that these methods will only give you enumerable properties. Typically those will be ones that are not built-in by the environment.
It is possible to add non-enumerable properties in ES5 supported browsers. These will not be included in
Object.keys, or when using afor-instatement.As noted by @Raynos, you can
Object.getOwnPropertyNames( window )for non-enumerables. I didn’t know that. Thanks @Raynos!So to see the values that include enumerables, you’d want to do this: