Skip to content

Get all Javascript Variables?

Is there a way for javascript to detect all assigned variables? For example, if one js file creates a bunch of vars (globally scoped), can a subsequent file get all the vars without knowing what they’re named and which might exist?

Thanks in advance 🙂

EDIT, Question Part 2:

How do I get the values of these variables? Here is what I have attempted:

This is what I ended up with, as per comment suggestions:

for (var name in this) {
    variables[name] = name;
    variables[name]=this[name]
}

Answer

Flanagan’s “JavaScript – The Definitive Guide” gives the following on page 653:

var variables = ""
for (var name in this)
    variables += name + "n";