一、遍历方式1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
var names = ["Lucy", "Lily"];
for(var i = 0; i < names.length; i++) {
console.log(names[i]); //Lucy Lily
}
//相当于Java中的传统for循环
// String[] names = {
// "Lucy",
// "Lily"
// };
// for(int i = 0; i < names.length; i++) {
// System.out.println(names[i]); //Lucy Lily
// }
</script>
</body>
</html>
一、遍历方式2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
var names = ["Lucy", "Lily"];
for(var i in names) {
console.log(names[i]); //Lucy Lily
}
//相当于Java中的加强for循环,不同之处在于JavaScript此方式遍历出的结果是下标值而不是元素值。
// String[] names = {
// "Lucy",
// "Lily"
// };
// for(String name: names) {
// System.out.println(name); //Lucy Lily
// }
</script>
</body>
</html>