Home »
JavaScript Examples
JavaScript | Code to declare an array and print using for each loop
Here, we are going to learn how to declare an array in JavaScript and how to access/print the array elements using for each loop?
Submitted by Pankaj Singh, on October 10, 2018
Declare an array and we have to print its elements/items using for each loop in JavaScript.
Code to declare an array and print using for each loop
<html>
<head>
<script>
var fruits = [
"apple",
"mango",
"banana",
"grapes",
"guava"
];
</script>
</head>
<body>
<script>
document.write("<h3>Array : Type 2</h3>")
document.write("<hr />");
document.write("<ul>");
fruits.forEach(function(item){
document.write("<li>"+item+"</li>");
});
document.write("</ul>");
</script>
</body>
</html>
Output
JavaScript Examples »