1

I have a code snippet where I am trying to remove some properties from an object.

let users = [{name: "a", prop1: 1, prop2: 2},{name: "b", prop1: 2, prop2: 2}, {name: "c", prop1: 3, prop2: 2}];

console.log("before deleting property", users);

for (var i = 0; i < users.length; i++) {
    delete users[i]['name'];
}

console.log("after deleting property", users);

When I run this code in NodeJS my output is:

before deleting property [ { name: 'a', prop1: 1, prop2: 2 },
  { name: 'b', prop1: 2, prop2: 2 },
  { name: 'c', prop1: 3, prop2: 2 } ]
after deleting property [ { prop1: 1, prop2: 2 },
  { prop1: 2, prop2: 2 },
  { prop1: 3, prop2: 2 } ]

However, when I am executing the same code in the browser (Google Chrome Version 73.0.3683.103 (Official Build) (64-bit) - Mac OS) the output changes (Property is removed in both the log messages):

Property is removed in both the log messages Related jsFiddle: https://jsfiddle.net/abhas9/f0erjhv2/12/

However, if I change the log message to say:

console.log("before deleting property", JSON.stringify(users));

I am getting the expected output.

Am I missing something? Is this browser's way of optimizing log statements in developer tools? If so, are there any related specifications?

Abhas Tandon
  • 1,859
  • 16
  • 26
  • https://console.spec.whatwg.org/ – Teemu Apr 10 '19 at 10:07
  • 2
    See the linked question's answers. It isn't executed later, but that's when you expanded the object that was logged, so you see the state of the object *then* (when you expanded it), not when it was logged. – T.J. Crowder Apr 10 '19 at 10:07
  • @Teemu Though, the specification for the relevant section is... no specification at all (implementation dependent :P) `The printer operation is implementation-defined.` I think browsers *could* deep log objects when logged, they just don't want to, for good reason – CertainPerformance Apr 10 '19 at 10:08
  • @CertainPerformance Well, it is standardized as implement dependent then = ). – Teemu Apr 10 '19 at 10:15

0 Answers0