Search code examples
javascriptfor-loopfor-in-loop

javascript "for (x in y)" statements


//just copied this code from w3schools
var person={fname:"John",lname:"Doe",age:25}; 

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

I want to know that, what I have to assume instead of "x".


Solution

  • The person is object and X is variable used in iteration of the for loop, you can name it anything other than X also :). Here X works as key of the object for example:

    alert(person["fname"]);
    

    Here fname is stored in X along with other keys such as lname and age.