javascript traverse the object's properties

Different types of cycles

JavaScript support different types of loops:

  • for - multiple passes through the block
  • for / in - traverse the object properties
  • while - when the specified condition is true when a code block cyclic
  • do / while - when a specified condition is true when a code block cyclic

== Look For / In cycle ==

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript 循环</h1>

<p>for/in 语句循环遍历对象的属性。</p>

<p id="demo"></p>

<script>
var txt = "";
var person = {fname:"Bill", lname:"Gates", age:62}; 
var x;
for (x in person) {
  txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
</script>

</body>
</html>

result:

Javascript 循环
for/in 语句循环遍历对象的属性。
Bill Gates 62

Guess you like

Origin www.cnblogs.com/heliusKing/p/11489244.html