Javascript for-in Statement

Introduction

The for-in statement is a strict iterative statement.

It is used to enumerate the non-symbol keyed properties of an object.

Here's the syntax:

for (property in expression) 
   statement 

And here's an example of its usage:

for (const propName in window) { 
  document.write(propName); 
} 

Here, the for-in statement is used to display all the properties of the BOM window object.

Each time through the loop, the propName variable is filled with the name of a property from the window object.

This continues until all of the available properties have been enumerated.

Object properties in Javascript are unordered.

The order in which property names are returned in a for-in statement cannot be predicted.

All enumerable properties will be returned once.

The for-in statement doesn't execute the body of the loop if the variable representing the object to iterate over is null or undefined.




PreviousNext

Related