If I create an object like this:
var obj = {};
obj.prop1 = "Foo";
obj.prop2 = "Bar";
Will the resulting object always look like this?
{ prop1 : "Foo", prop2 : "Bar" }
That is, will the properties be in the same order that I added them?
If I create an object like this:
var obj = {};
obj.prop1 = "Foo";
obj.prop2 = "Bar";
Will the resulting object always look like this?
{ prop1 : "Foo", prop2 : "Bar" }
That is, will the properties be in the same order that I added them?
Current Language Spec: technically, order is unspecified.
Current Browsers: order is preserved with the major exception of keys like "7" that parse as integers and are handled differently by Chrome/V8.
Future Language Spec (>ES2015): Generally, you can expect that things ordered today will not become unordered. New APIs will guarantee order; existing APIs are difficult to change. See JMM's answer for more details.
The best link below is in Tim Down's comment:
http://code.google.com/p/v8/issues/detail?id=164
That bug covers in detail the design decisions involved for Chrome's implementation of key ordering. One take-away is that for string keys that don't parse to an integer (ie "a" or "b", but NOT "3"), keys are printed in insertion order on all major browsers and while this behavior is not "standardized", it IS considered a significant backwards-compatibility issue by browser vendors. Use at your own risk.
Per one of the (rather opinionated) comments:
If you rely on insertion order, you are outside the ECMAScript spec, but within the de-facto standard of common browser behavior as long as your keys don't parse as integers.
At the time of writing, most browsers did return properties in the same order as they were inserted, but it was explicitly not guaranteed behaviour so shouldn't have been relied upon.
The ECMAScript specification used to say:
However in ES2015 and later non-integer keys will be returned in insertion order.
No, properties order in objects is not guaranteed in JavaScript; you need to use an
Array
.Definition of an Object from ECMAScript Third Edition (pdf):
Since ECMAScript 2015, using the
Map
object could be an alternative. AMap
shares some similarities with anObject
and guarantees the keys order:As others have stated, you have no guarantee as to the order when you iterate over the properties of an object. If you need an ordered list of multiple fields I suggested creating an array of objects.
This way you can use a regular for loop and have the insert order. You could then use the Array sort method to sort this into a new array if needed.
This whole answer is in the context of spec compliance, not what any engine does at a particular moment or historically.
Generally, no
The actual question is very vague.
In what context?
The answer is: it depends on a number of factors. In general, no.
Sometimes, yes
Here is where you can count on property key order for plain
Objects
:Object.getOwnPropertyNames()
,Reflect.ownKeys()
,Object.getOwnPropertySymbols(O)
In all cases these methods include non-enumerable property keys and order keys as specified by
[[OwnPropertyKeys]]
(see below). They differ in the type of key values they include (String
and / orSymbol
). In this contextString
includes integer values.Object.getOwnPropertyNames(O)
Returns
O
's ownString
-keyed properties (property names).Reflect.ownKeys(O)
Returns
O
's ownString
- andSymbol
-keyed properties.Object.getOwnPropertySymbols(O)
Returns
O
's ownSymbol
-keyed properties.[[OwnPropertyKeys]]
The order is essentially: integer-like
Strings
in ascending order, non-integer-likeStrings
in creation order, Symbols in creation order. Depending which function invokes this, some of these types may not be included.The specific language is that keys are returned in the following order:
Map
If you're interested in ordered maps you should consider using the
Map
type introduced in ES2015 instead of plainObjects
.Property order in normal Objects is a complex subject in Javascript.
While in ES5 explicitly no order has been specified, ES2015 has an order in certain cases. Given is the following object:
This results in the following order (in certain cases):
Thus, there are three segments, which may alter the insertion order (as happened in the example). And integer-like keys don't stick to the insertion order at all.
The question is, for what methods this order is guaranteed in the ES2015 spec?
The following methods guarantee the order shown:
The following methods/loops guarantee no order at all:
Conclusion: Even in ES2015 you shouldn't rely on the property order of normal objects in Javascript. It is prone to errors. Use
Map
instead.