HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

The For In Loop

The JavaScript for in statement loops through the properties of an Object:

Syntax

for (key in object) {
// code block to be executed
}

Example

 <!DOCTYPE html>
<html>
<body>
<h2>JavaScript For In Loop</h2>
<p>The for in statement loops through the properties of an object:</p>
<p id="demo"></p>
<script>
// Define an object named 'person' with properties const person = {fname:"John", lname:"Doe", age:25};
// Initialize an empty string 'txt' let txt = "";
// For...in loop to iterate through the properties of the 'person' object for (let x in person) {
// Concatenate the value of each property to the 'txt' string txt += person[x] + " ";
} // Display the result in the paragraph with id "demo" document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
You can click on above box to edit the code and run again.

Output