JavaScript for...in Statement


The for...in statement loops through the properties of an object. The code in the body of the for...in loop is executed once for each element/property.

The variable argument can be a named variable, an array element, or a property of an object.
 for ( variable in object ) {
   code to be executed
 }

The script allows users to guess a digit. It quits after five tries. The variable d2 starts with a value 0. Each time the for statement is executed, d2 will be increased by 1 until it reaches to 5 and then the script stops. The object Array lets you work with arrays. Its constructor includes



   

   new  Array( arrayLength );
   new  Array( element0, element1, ..., elementN );
You can refer to a particular element in an array by referring to the name of the array and the index number such as myDigit[d2]. The index number starts at 0.


Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.