Explain Codes LogoExplain Codes Logo

Does JavaScript guarantee object property order?

javascript
javascript-objects
property-order
es2015
Nikita BarsukovbyNikita Barsukov·Feb 5, 2025
TLDR

The quick take on JavaScript property order is:

  • Numeric keys appear first (sorted ascending).
  • String keys follow insertion order.
  • Symbols place last (also preserving insertion order).

Meaning, an object { '3': 'c', '1': 'a', 'b': 'b' } is processed as: 1, 3, b.

Check this in action:

let obj = { '3': 'c', '1': 'a', 'b': 'b' }; console.log(Object.keys(obj)); // ['1', '3', 'b'] // At least I didn't have to alphabetize this! 😅

Numeric properties ('1', '3') are first (numerically sorted), followed by string 'b', in insertion order.

Understanding JavaScript object property order

Integer-like and string keys in objects

In JavaScript, if your object properties are all strings, they will maintain the insertion order after ES2015. However, if they are numeric (integer-like) strings, they are sorted.

let obj = { '100': 'a', '2': 'b', '10': 'c' }; console.log(Object.keys(obj)); // ['2', '10', '100'] // That escalated slowly...

The "integer-lookalike" keys ('2', '10', '100') sorted like numbers (ascending).

Peter Parker 'Symbol' and its adaptable friends

Symbols retain their insertion order. And if you work with mixed keys (string, numeric, and symbol), the keys 'assemble' respecting their types:

let obj = { [Symbol('s')]: 'symbol', '2': 'b', 'c': 'string', '1': 'a' }; console.log(Reflect.ownKeys(obj)); // ['1', '2', 'c', Symbol(s)] // Wait, did the Numeric Avengers assemble before the String and Symbol League? 😄

Numeric-like keys ('1', '2') are listed first, then the string 'c', with the Symbol coming in last. Can this make the Avengers v/s Justice League debate more interesting?

The fluctuating landscape of object properties

Objects have their peculiar behavior, and it's crucial to adapt:

  • Map differently than Object: Map maintains insertion order, while Objects have a type-based order.
  • Browsers' discretion: Property enumeration wasn't standardized before ES2015– browsers had their style!
  • Crux of Chrome/V8: Chrome's V8 engine may sort numeric keys as a 7-year-old arranges his toys– fiercely guarded yet chaotic!

Systematic breakdown of property order

Enumeration methods and the JavaScript property order

JavaScript's enumeration methods like Object.keys(), Object.entries(), Object.values() follow this order

  1. Integer-like keys: Ascending number order
  2. Actual string keys: Insertion order
  3. Symbol keys: Last, preserving insertion order

These methods ensure iteration results will follow this lineup.

Special considerations and knick-knacks

  • Non-enumerable properties: Methods like Reflect.ownKeys() and Object.getOwnPropertyNames() list these, following the type-wise order.
  • Unchanged property order: Even if properties are added/modified using defineProperty or defineProperties, the order is firm.
  • Property reassignment: This doesn't affect the original order of properties.

Order! But your environment may vary

Though ECMAScript spells out property order, all environments might not follow:

  • Wildcard older browsers may show different behaviours.
  • Server-side platforms may have unique rules.

Alternatively, use a Map or an array.