JS: Order of Properties

By Xah Lee. Date: . Last updated: .

Order of properties

Object's properties are kept in this order:

  1. Positive integer-like string keys between "0" and 2**32 - 2 inclusive, in integer ascending order. (these are array indexes)
  2. Other String keys, in creation order.
  3. Symbol keys, in creation order.
const xx = {
 2: 1,
 b: 1,
 1: 1,
 a: 1,
 0: 1,
 "-1": 1,
};

console.log(Object.keys(xx));
// [ "0", "1", "2", "b", "a", "-1" ]

/*
note.
0 1 2 etc are moved to front.
-1 is not moved to front.
Other string keys, stays in creation order.
 */
/* demo showing the object property key order has a special case when the key is integer-like larger than array max index */

// this is max array index
console.assert(2 ** 32 - 2 === 4294967294);

const xx = {
 a: 1,
 4294967294: 1,
 4294967295: 1,
};
console.log(Object.keys(xx));
// [ "4294967294", "a", "4294967295" ]

/*
 4294967294 is moved to front.
 4294967295 is not.
*/

History of order of properties

ECMAScript 2015 first specified an order for Object.getOwnPropertyNames . ECMAScript 2020 specified that more functions must return that order.