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 );
arrayLength
: the initial length of the array.
You can access this value using the length
property.
If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.
elementN
: a list of values for the array’s elements.
When this form is specified, the array is initialized with the specified values as its elements, and the length
property is set to the number of arguments.
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.