How to get array structure with alert() in JavaScript?

Here is the code to see the array structure using alert() . Here below few techniques
are discussed
Approach 1:

  • First take the values in a variable(lets arr).
  • Pass the array name in the alert() .
  • We can directly use the array name because arrayName automatically converted to arrayName.toString()

Example 1: This example follows the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to get array structure 
        with alert() in JavaScript?
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;"
            GeeksForGeeks 
    </h1>
    
    <p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
    </p>
    
    <button onclick="gfg_Run()">
        Click here
    </button>
    
    <script>
        var el_up = document.getElementById("GFG_UP");
        var arr = [1, 2, 4, 6, 9];
        el_up.innerHTML = 
"Click on the button to see the array structure using Alert().<br> Array is = "
        + arr;
  
        function gfg_Run() {
            alert(arr);
        }
    </script>
</body>
  
</html>

chevron_right


Output:



  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • First take the values in a variable(lets arr).
  • Pass the array name in the alert() .
  • We can use .join() method for our simplicity to see the array elements each in a line.

Example 2: This example follows the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to get array structure
        with alert() in JavaScript?
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
    <p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
    </p>
    <button onclick="gfg_Run()">
        Click here
    </button>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var arr = [1, 2, 4, 6, 9];
        el_up.innerHTML = 
"Click on the button to see the array structure using Alert().<br> Array is = "
        + arr;
  
        function gfg_Run() {
            alert(arr.join('\n'));
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.