View the list of all variables in Google Chrome Console using JavaScript

20 Sep, 2019
All the variables in Google Chrome can be listed for the use of debugging. There are two approaches to list all variables:
Method 1: Iterating through properties of the window object: The window object in JavaScript represents the current browser’s window. The properties of this object can be used to find the variables of the Chrome browser. Each of the properties of the window object is first checked with the hasOwnProperty() method. This ensures that the object has the property as its own property.
Syntax:
for (let variable in window) {    if (window.hasOwnProperty(variable)) {        console.log(variable);    }}
Example:
<!DOCTYPE html><html>  <head>    <title>        View the list of all variables in Google        Chrome Console in JavaScript    </title></head>  <body>    <h1 style="color:green;">        GeeksforGeeks    </h1>          <b>        View list of all JavaScript variables        in Google Chrome Console    </b>          <p>        Click on the button to view list of        all JavaScript variables in the        Google Chrome Console    </p>          <button onclick="findAllVariables()">        Click here    </button>          <script type="text/javascript">        function findAllVariables() {            for (let variable in window) {                if (window.hasOwnProperty(variable)) {                    console.log(variable);                }            }        }    </script></body>  </html>
Output:Console Output:
Method 2: Using the Object.keys() method: The Object.keys() method is used to return the properties of the given object as an array. As the window object represents the current browser’s window, the properties of this object can be used to find the variables like the previous method.The Object.keys() method is passed the window object as the parameter to get its keys. Each of the keys in this object represents a variable of the Google Chrome browser. These can then be listed in the console.
Syntax:
let variables = Object.keys(window);console.log(variables);
Example:
<!DOCTYPE html><html>  <head>    <title>        View the list of all variables in Google        Chrome Console in JavaScript    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        View list of all JavaScript variables        in Google Chrome Console    </b>          <p>        Click on the button to view list        of all JavaScript variables in         the Google Chrome Console    </p>          <button onclick="findAllVariables()">        Click here    </button>          <script type="text/javascript">        function findAllVariables() {            let variables = Object.keys(window);            console.log(variables);        }    </script></body>  </html>
Output:Console Output:
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?