How to list variables defined inside a scope in JavaScript?


How do I know/list which variables have already been defined within a scope, whether global or local?

For example if I set var x, y, z = 10 , the result of a possible command to list the variables already defined should be something like an array with their names: ["x", "y", "z"] .

Is it possible to do this in JavaScript?

In R, for example, this command would be ls() and would return an array of strings with variable names.


As @bfavareto also mentioned this is not possible in Javascript. However, if you plan your code well using a placeholder you can do this check. However, variables defined inside functions are not accessible to outside scopes.

var namespace = {
    minhafuncao1: function () {

    minhavariavel1: 100

for (variable in namespace) {

From the:

Scroll to Top