JavaScript: Enumerating Properties of an Object with a For/In Loop

JavaScript object properties can be enumerated by using a for/in loop, as demonstrated below:

var objCar = new Object();
objCar.wheels = 4;
objCar.tires = 4;
objCar.engines = 1;

for (var i in objCar)
{
document.write(i);
document.write('<br/>');
}

The result of this code is:

wheels
tires
engines

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.