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

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

for (let variable in window) {
    if (window.hasOwnProperty(variable)) {
        console.log(variable);
    }
}

chevron_right


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:
in-window-output
Console Output:
in-window-console

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

let variables = Object.keys(window);
console.log(variables);

chevron_right


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:
objectkeys-output
Console Output:
objectkeys-console



My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.