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> |
chevron_right
filter_none
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> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript Course | Understanding Code Structure in JavaScript
- How to pop an alert message box using PHP ?
- How to add a new line in the alert box ?
- HTML | Window alert( ) Method
- How to load notification alert on top right corner without click of button in bootstrap ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to get the elements of one array which are not present in another array using JavaScript?
- How to check whether an array is subset of another array using JavaScript ?
- JavaScript | Array pop()
- RMS Value Of Array in JavaScript
- JavaScript | Array every() function
- How to Convert Array to Set in JavaScript?
- JavaScript | array.flatMap()
- JavaScript | Array concat()
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.



