Tuesday, July 28, 2026
HomeLanguagesJavascriptHow to get array structure with alert() in JavaScript?

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.




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


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.




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


Output:

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

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS