欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js 学习之路8:for循环

程序员文章站 2022-07-03 17:50:07
1. for循环 结果: 2. for/in循环直接遍历循环对象的属性。 结果: ......

1. for循环

<!doctype html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<body>

<script charset = "utf-8">

namelist = ["kl", "td", "mgeno", "bobo"];
for (var i = 0; i < namelist.length; i++)
{
    document.write(namelist[i] + "<br>");
}
</script>

</body>
</html>

结果:

js 学习之路8:for循环

 

2. for/in循环直接遍历循环对象的属性。

<!doctype html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<body>

<script charset = "utf-8">

var maninfo = {name: "tim", age: 20, grade: 98};
for (var i in maninfo)
{
    var info = i + ": " + maninfo[i];
    document.write(info + "<br>");
}

</script>

</body>
</html>

结果:

js 学习之路8:for循环