0

The problem which I don't understand it that when I console.log the first time, it also has added the Array (extra with the key and value (welcome and '2') How is this possible?

var testarray = {
  "welcome": "1"
}
console.log('start');
console.log(testarray); // Shows also the extra array already.
console.log('end');

testarray.extra = [];
testarray.extra.welcome = '2';
Jamiec
  • 133,658
  • 13
  • 134
  • 193
nhatimme
  • 383
  • 3
  • 19
  • 1
    I made your code runable, and it does not exhibit the behavior you describe. Please update it to show a [mcve] of the issue you have. – Jamiec Nov 02 '21 at 11:51
  • 2
    Also, note that the variable you call `testarray` is not actually an array. It's an object. – Jamiec Nov 02 '21 at 11:52
  • Ah yeah I always forget that the chrome console behaves differently than the SO text based console. The targetted duplicate answers your question perfectly – Jamiec Nov 02 '21 at 11:53
  • 2
    You're seeing [this feature of browser consoles](http://stackoverflow.com/questions/38660832/element-children-has-elements-but-returns-empty-htmlcollection). The `extra` property **isn't** there as of when you log the object, but it *is* there when you click the arrow to expand the object in the console later. Browser consoles keep a live reference to the object and if they show you a summary with an expander icon, what you see when you click the icon is the object's contents *when you clicked*, not earlier when it was logged. – T.J. Crowder Nov 02 '21 at 11:53
  • if you mean the console in dev-tools, your object was evaluated and changed later. see this post https://stackoverflow.com/questions/42031634/chrome-displays-different-object-contents-on-expand – Sebastian Nov 02 '21 at 11:54
  • It's just visual, if you try to access testarray.extra before you define it it will be undefined – Gabriel Nov 02 '21 at 11:55

0 Answers0