SiriusKoder
SiriusKoder

Reputation: 341

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".

Upvotes: 1

Views: 18476

Answers (2)

Duncan MacLeod
Duncan MacLeod

Reputation: 131

You want to have

for ( x in Object.keys(person)) {
  console.log(person[x]);
}

this will give you a list of KEYS rather than a list of values.

Upvotes: 6

Sarfraz
Sarfraz

Reputation: 382776

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.

Upvotes: 1

Related Questions