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