a = 123; // becomes global var
var b = 234; // local var
function f() {} // local func
g = function() {} // got a global func
now how to print
All global variables are stored on the window
object in a browser. Alternatively, they are stored on the global
or GLOBAL
object in Node.js. You can enumerate that object's keys instead, but keep in mind that it will contain much, much more than user-defined globals.
Globals become attached to the global object, which in a browser is the window.
a = 123
is the same as
window.a = 123
How about:
var globals = window || global;
for (var key in globals){
console.log(window[key]);
}