Why does for(var i in Math) not iterate through Math.* in Javascript?

Because Math is a built in object whose properties are flagged non-enumerable. Many built in objects have this behavior, which is why looping over an array with for..in will not give you problems until Array.prototype is extended with user functions, which are always enumerable by default.

Until recently non-enumerable was an internal property not accessible by regular Javascript code. However EMCAScript 5 specifies the ability to set the enumerability and writeability (try changing the value of Math.PI) of any object property through Object.defineProperty().

It also provides Object.getOwnPropertyNames() as a way to get a list of all properties of an object regardless of their enumerability.

Object.getOwnPropertyNames(Math);

//returns
["LN10", "PI", "E", "LOG10E", "SQRT2", "LOG2E", "SQRT1_2", "LN2", "cos", "pow", "log", "tan", "sqrt", "ceil", "asin", "abs", "max", "exp", "atan2", "random", "round", "floor", "acos", "atan", "min", "sin"]

Far as I know the only browsers that currently support these functions are Chrome and Safari. Firefox should support it at version 4. IE9 I am not sure about, but Microsoft has stated they intend to support the EMCAScript 5 standard eventually.

I do not believe there is any way to emulate this functionality in Javascript interpreters without explicit support.


As with most built-in objects in JavaScript, properties and methods of the Math object are defined in the ECMAScript spec (section 15.8.1) as not being enumerable via the (inaccessible to script) DontEnum attribute. In ECMAScript 5, you can mark properties and methods of your own objects as being non-enumerable also:

var o = {};

Object.defineProperty(o, "p", {
    enumerable: false,
    value: 1
});

Object.defineProperty(o, "q", {
    enumerable: true,
    value: 2
});

for (var i in o) {
    console.log(i + "=>" + o[i]);
}
// q=>2

Those properties are non-enumerable.

From the MDC documentation on for..in:

A for...in loop does not iterate over built-in properties.

In newer JavaScript implementations you can make your own non-enumerable properties. Check out propertyIsEnumerable() and Object.defineProperty().

Tags:

Javascript