Declare an array and we have to print its elements/items using for each loop in JavaScript.
聲明一個數組,我們必須使用JavaScript中的每個循環來打印其元素/項目。
Code:
碼:
<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
輸出量

翻譯自: https://www.includehelp.com/code-snippets/code-to-declare-an-array-and-print-using-for-each-loop-in-javascript.aspx