Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:


Last Updated : 06 Sep, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads