Javascript/Vòng lặp/Vòng lặp For .. in

From Wikiversity
Jump to navigation Jump to search

Cú pháp[edit]

for (var in  Đối tượng) {
 Dòng lệnh
}

Thí dụ[edit]

<!DOCTYPE html>
<html>
<body>

<p>Click the button to loop through the properties of an object.</p>

<button onclick="myFunction()">Try it</button>

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

<script>
function myFunction() {
  var person = {fname:"John", lname:"Doe", age:25}; 
  
  var text = "";
  var x;
  for (x in person) {
    text += person[x] + " ";
  }
  document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>