be8mba
Last Updated: February 25, 2016
·
571
· pose

Error properties are not enumerable in Javascript

If you transverse a regular object:

var obj = {foo: 'bar', baz: true};

for (var i in obj) {
  console.log(i, obj[i]);
}

The following will be output:

foo bar
baz true

But, what if you transverse an instance of Error:

var e = new Error('Something went wrong');
for (var i in e) {
  console.log(i, e[i]);
}

That won't print anything. In addition, Object.keys(e) will return [].

Let's add some custom properties.

var e = new Error('Something went wrong');
e.foo = 'bar';
for (var i in e) {
  console.log(i, e[i]);
}

Then those will be displayed:

foo bar

Confused? Then check Error default properties are not enumerable.