0

I have an Object like this.

let code ={
    2: 'name',
    name: 'name',
    1: 'name',
    key: 'name',
    0: 'name'
}

When we try do console.log, we get a value as below.

{
   0: "name"​
   1: "name"
​   2: "name"
   key: "name"
   name: "name"
}

Basically the number keys are getting sorted and the remaining keys are getting printed in the same order that we added. Can i know the reason behind this action? And how does this is happening behind the scene. Please explain in detail.

user2613946
  • 435
  • 2
  • 7
  • 17
  • There is no guarantee of key/value pair order in objects. This is normal behavior in JavaScript. If you need to rely on order, convert your object to an array and sort based on whatever criteria you need. – Derek Jun 03 '23 at 10:02
  • Because that's how the order of JavaScript object properties is defined (now), and whatever you're using to look at the object is following the defined order. But the order is complex; don't rely on property order in objects. If you need order, use an array. (Or a Map or Set, which are also ordered in JavaScript, and with much less complex rules than object properties.) – T.J. Crowder Jun 03 '23 at 10:03
  • @Derek - JavaScript properties have had a defined order for years now, starting in 2015 with some caveats, and now without any significant caveats. (But that doesn't mean relying on that order is a good idea. :-) ) – T.J. Crowder Jun 03 '23 at 10:03
  • @Derek I am not relying on the order but just trying to understand why does this happen in object and how is this happening? – user2613946 Jun 03 '23 at 10:06
  • @user2613946 - It's literally just that there's an order applied when you loop through an object's properties, and whatever you're using to look at the above is following that order (which is useful). The rules are complex, but fairly simple if we ignore inherited properties. In that case it's: First, the properties whose names are strings that contain only digits forming numbers in normal form, in order numerically; then other properties whose names are strings, in the order they were added to the object; and then properties whose names are Symbols in the order they were added. – T.J. Crowder Jun 03 '23 at 10:08
  • (I should note that your object has only properties whose names are strings. Even though you used numbers when specifying those property names, they're automatically converted to string. Object property keys are always either strings or Symbols.) – T.J. Crowder Jun 03 '23 at 10:12
  • @T.J.Crowder Got it. This might seems silly. But asking out of curiosity. So the question is, where can i see those rules? – user2613946 Jun 03 '23 at 10:12
  • @user2613946 - In the specification. It's handled in a couple of different places. For own properties, [it's here](https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-ordinaryownpropertykeys). For the total ordering including inherited properties, [it's here](https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-enumerate-object-properties). – T.J. Crowder Jun 03 '23 at 10:16
  • @T.J.Crowder Can you please answer this? https://stackoverflow.com/questions/76395847/what-is-the-algorithm-applied-to-create-a-circular-array-in-javascript?noredirect=1#comment134712707_76395847 – user2613946 Jun 03 '23 at 11:44

0 Answers0