How to get Array Structure with alert() in JavaScript?
Last Updated :
12 Jul, 2025
Improve
To display an array structure using alert() in JavaScript, the array can be converted into a string format Array.toString(). This allows the contents of the array to be shown in a readable format within an alert box.
Below are the approaches to get array structure with alert() in JavaScript:
Table of Content
Approach 1: Using arrayName.toString() method
- First, take the values in a variable(let arr).
- Pass the array name in the alert().
- We can directly use the array name because arrayName is 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>
let el_up = document.getElementById("GFG_UP");
let 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>
<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>
let el_up = document.getElementById("GFG_UP");
let 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:

Approach 2: Using join() method
- 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: 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>
let el_up = document.getElementById("GFG_UP");
let 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>
<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>
let el_up = document.getElementById("GFG_UP");
let 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:
