I was talking about hasOwnProperty with another developer and how you are supposed to use it in for-in loops in javascript and he had a good question. When you do a for-in loop, why doesnt toString, hasOwnProperty, and other built in methods show up in the loop?
-
3I'm pretty sure these methods have the internal *enumerable* attribute set to `false` but I cannot find any reference. Maybe because `Object.prototype` is marked this way. See http://ecma262-5.com/ELS5_HTML.htm#Section_15.2.3 – Felix Kling May 27 '11 at 17:38
-
@Felix - that's exactly the answer. – Spudley May 27 '11 at 17:40
-
1@Felix, http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/. Make that an answer. – hyperslug May 27 '11 at 17:41
-
can we utilize this enumerable attribute ourselves or is that internal only? – Allen Rice May 27 '11 at 17:43
-
@Allen: In the newest version (ES 5) you can. And I have to revise my comment: It is not because of `Object.protoype`. If you add custom properties to it, they show up in the loop afaik. – Felix Kling May 27 '11 at 17:44
-
@Felix, yes if you add to `Object.prototype` they show up in a for-in, however, if you redefine `Object.prototype.hasOwnProperty = "YES"`, it will not show up, at least not in IE<9, from my understanding, it should then show up in IE9 or greater or other browsers though. This is discussed in that awesome post that hyperslug linked to. – Allen Rice May 27 '11 at 17:49
5 Answers
The ECMAScript defines several properties for each property found on objects such as in prototypes. One of these is the enumerable
property, and if it is set to false
, then that property will be skipped.
You can actually manipulate these properties using the defineProperty
function:
This method allows precise addition to or modification of a property on an object. Normal property addition through assignment creates properties which show up during property enumeration (for...in loop), whose values may be changed, and which may be deleted. This method allows these extra details to be changed from their defaults.

- 1,048,767
- 296
- 4,058
- 3,343
It is per specification
A for...in loop does not iterate over built-in properties. These include all built-in methods of objects, such as String's indexOf method or Object's toString method. However, the loop will iterate over all user-defined properties (including any which overwrite built-in properties).
From Mozilla Developer Network page on for..in
It is internally based on the enumerable attribute of these properties, as you can check in the EcmaScript specification (search for "for-in", the "enumerable" attribute is described page 30)

- 11,779
- 4
- 39
- 51
I'm pretty sure these methods have the internal [[Enumerable]] attribute set to false
but I cannot find anything where this is explicitly stated.
Update: Apparently, being non-enumerable is the default setting for properties defined in the specification (if not specified otherwise) (see table 7 in the link below).
You can find more information about these attributes in the specification: Property attributes:
If
true
, the property will be enumerated by a for-in enumeration (see 12.6.4). Otherwise, the property is said to be non-enumerable.

- 795,719
- 175
- 1,089
- 1,143
-
Just download the ECMAScript PDF and search; there are preciously few HTML versions of the document and the ECMA does not maintain such a version themselves. – Martijn Pieters May 27 '11 at 17:46
-
@Martijn: I had a look at it. But the specifiction of these methods that not state that this attribute is set to false. Maybe it is somewhere written in the text. – Felix Kling May 27 '11 at 17:49
-
Section 8.6.1, tables 5 through to 7 (the latter defines the defaults). – Martijn Pieters May 27 '11 at 17:50
-
@Martijn: Ah, ok then it is because it is the default value.... well, sometimes it is explicitly stated that the property is *not* enumerable, so this is a bit confusing. – Felix Kling May 27 '11 at 17:52
-
That's the ECMA standards for you. :-) Ever tried to read the whole thing? – Martijn Pieters May 27 '11 at 17:53
-
@Martijn: I started it, but somehow I got better things to do then ;) – Felix Kling May 27 '11 at 17:53
Built-in properties aren't enumerable therefore toString
and hasOwnProperty
aren't enumerable. In ECMAScript 3 every user defined method or property is enumerable. In ECMAScript 5 you can choose if method or property would be enumerable.

- 2,742
- 20
- 19
Perhaps I misunderstand your question, but here's an example of hasOwnProperty working from inside a for-in loop:
var i,o={some:"thing"};for(i in o)alert(o.hasOwnProperty("some"));

- 14,629
- 4
- 30
- 53
-
I'm not asking about how to use it, I'm saying its obviously a member of every objects prototype but it never shows up in a for-in loop. Lepidosteus has the right answer. – Allen Rice May 27 '11 at 17:41
-