Short answer: JavaScript has no concept of querying the list of variables defined as const
or let
.
You can however query the list of variables defined with var
because they are attached to the window
object. This is the windowKeys
example below.
If you want to query the variables why not adding them to an object? This is the myObjectKeys
example below.
var _Tasks = {};
var _client = {};
var _client_simple = {};
var _a_number = 123;
var _a_string = "hello";
var _a_boolean = true;
const windowKeys = Object.keys(window).filter(key => /^_/.test(key));
console.log('windowKeys:');
windowKeys.forEach(key => {
console.log(' ' + key + ', type: ' + typeof window[key]);
});
const myObject = {
_Tasks: {},
_client: {},
_client_simple: {},
_a_number: 123,
_a_string: "hello",
_a_boolean: true
}
const myObjectKeys = Object.keys(myObject);
console.log('myObjectKeys:');
windowKeys.forEach(key => {
console.log(' ' + key + ', type: ' + typeof myObject[key]);
});
Output:
windowKeys:
_Tasks, type: object
_client, type: object
_client_simple, type: object
_a_number, type: number
_a_string, type: string
_a_boolean, type: boolean
myObjectKeys:
_Tasks, type: object
_client, type: object
_client_simple, type: object
_a_number, type: number
_a_string, type: string
_a_boolean, type: boolean