JS: Order of Properties

By Xah Lee. Date: . Last updated: .

Order of properties

(new in ECMAScript 2020)

Object's properties are kept in this order:

  1. First is Integer-like string keys, including "0" (aka Array index keys), in integer ascending order.
  2. Other String keys, in creation order.
  3. Last is 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.
 */