{
  "id": 23734266,
  "title": "Does Object.keys keep your insert order in JavaScript?",
  "url": "https://urgent.news/2026/12/06/does-object-keys-keep-your-insert-order-in-javascript",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-12-06T00:39:49.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/shipstackai/does-objectkeys-keep-your-insert-order-in-javascript-28ff"
  },
  "original_language": null,
  "account": "When assigning keys \"b\", 2, \"a\", and 1 to a plain JavaScript object, `Object.keys` returns the keys in the order of 1, 2, \"b\", \"a\". This behavior is intentional and not a bug; the specification divides keys into two groups and sorts one of them. The integer-like keys \"1\" and \"2\" are sorted in ascending numeric order, while the string keys \"b\" and \"a\" follow in the order they were inserted. Integer keys, such as \"01\", \"1.5\", and \"-1\", are excluded from the integer group and remain in the string group. If relying on insertion order is crucial, consider using a Map, as it maintains the insertion order for all key types, including integer-like keys. In the provided examples, a plain object and a Map with the same keys were used to illustrate the difference in key ordering.",
  "summary": "Assign the keys b , 2 , a , 1 to a plain object and Object.keys comes back as 1, 2, b, a . That is not a bug and it is not random: the spec splits keys into two groups and sorts one of them. The test const o = { } ; o . b = 1 ; o [ 2 ] = 2 ; o . a = 3 ; o [ 1 ] = 4 ; console . log ( Object . keys ( o ) ) ; // [ '1', '2', 'b', 'a' ] console . log ( JSON . stringify ( o ) ) ; //…",
  "summary_source": "publisher",
  "key_points": [
    "Object.keys returns keys in numeric order first, then insertion order.",
    "Integer-like keys like \"1\" and \"2\" are sorted numerically.",
    "Maps maintain insertion order for all key types, including integers."
  ],
  "editors_take": "Developers who rely on insertion order for all key types should use a Map to avoid the automatic numeric sorting applied to plain JavaScript objects.",
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "outlets_capped": false,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}