JavaScript For…In Statement

JavaScript For…In Statement

The for…in statement is used to loop (iterate) through the elements of an array or through the properties of an object. The for…in statement iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements. A for…in statement looks as follows:

Syntax

for (variable in object)
{
code to be executed
}

Note: The code in the body of the for…in loop is executed once for each property.

Example

Looping through the properties of an object:

var person={fname:”Ali”,lname:”Dua”,age:25};

for (x in person)
{
document.write(person[x] + ” “);
}

Output

Ali Dua 25

DOWNLOAD SOLUTION HERE