Enumerable in JavaScript

Object.defineProperty(obj, 'answer', {
  value: 42,
  writable: true,
  enumerable: true,
  configurable: true
});

The property enumerable specifies a specific property of an object to be available for operations.

The default properties of inbuilt objects in JavaScript are not enumerable that means they can not be listed by using for in loop.

When you try to get the properties of an object, the default properties are not being listed because of enumerable false assigned to them. These properties are not listed when you try to get the properties of that object.

var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';

o.propertyIsEnumerable('prop');   // returns true
a.propertyIsEnumerable(0);        // returns true
a.propertyIsEnumerable('length');   // returns false

One can check if the property is enumerable by using propertyIsEnumerable. The length is a default property of an array object. But it is not listed when you try to get the keys/properties in an array.

Advertisements

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 )

Google+ photo

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

Connecting to %s