Thursday, September 4, 2025
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
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS