myObj.FirstName = 'Phillip',
myObj.LastName = 'Senn';
for (var X in myObj) // FirstName LastName
for each (var X in myObj) // Phillip Senn
Q: Mentally, how do you read these two statements?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first one (
for ( in )) is reading property names from the object.So you may read it as for each property in
myObj, assign it tox.The second one (
for each ( in )) is reading values of the properties in the object.This one may be read as for each property’s value in
myObj, assign it tox.Note that
for eachhas limited browser support.Also note that if extra properties are appearing in
for ( in ), it is because it will look up the prototype chain for extra enumerable properties (and someone may have augmentedObject, for example).You can mitigate this with…
jsFiddle.