How to get all global JavaScript variables?

Sometimes, we want to get all JavaScript global variables.

In this article, we’ll look at how to get all JavaScript global variables.

How to get all JavaScript global variables with JavaScript?

To get all JavaScript global variables, we can loop through the window object’s properties.

For instance, we write:

for (const [key, val] of Object.entries(window)) {
  console.log(key, val)
}

to call Object.entries with window to return an array of key-value pair in window as arrays.

In the loop, we log the key and val we destructured from the array.

Conclusion

To get all JavaScript global variables, we can loop through the window object’s properties.