Categories
JavaScript Answers

How to get all global JavaScript variables?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *