JS: Order of Properties

By Xah Lee. Date: .

Order of Properties

New in JS: ECMAScript 2020

Object's properties are kept in this order:

  1. Integer-like string keys, including "0" (aka Array index keys), In integer increasing order.
  2. Other String keys, in creation order.
  3. Symbol keys, in creation order.
const xx = {
  2: "x",
  b: "x",
  1: "x",
  a: "x",
  0: "x",
  "-1": "x",
};

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.
 */

JavaScript, Property