2

If i run the following code on chrome or firefox directly into the console, on the first log the value of value2 is 0 in the inlined representation, but if I expand the representation to check the whole object, it is showed as 17. Is this a multi browser bug or I am missing something with javascript here? The reason I am asking this question here is because this behaviour is happening across different browsers, which makes me think there is some kind of catch on the language or the function that I am not aware of.

Actual code:

action = new Object();
action.value1 = 17;
action.value2 = 0;

console.log(action);
action.value2 = action.value1;
console.log(action);

Tested on MacOS Catalina with the latest version of both browsers.

EDIT-----------------

The problem is the first console.log prints value2 being 0 but when I expand the object details it is showed as 17. I attached a print to clarify the question.

PRINT OF THE CONSOLE RESULTS

I would expect the result to be 0 on the value2 of the first output even with the expanded view.

  • You might need to clarify this if the answer below doesn't help. Seems like you don't understand what should be happening with your code. – Rodger Feb 28 '20 at 02:16

1 Answers1

1

console.log has different inner working between chrome, firefox, nodejs, and other javascript engine.

In your screenshot, the browser does 2 things:

  1. prints the short preview of the action object
  2. prints the expandable reference of the action object

The short preview is a readable version snapshot of the object at the time console.log is called

The expendable reference only prints an expand icon at the time console.log is called. When expanded, chrome will fetch the CURRENT value the object referenced by that expendable reference and print it. It doesn't print the value when console.log is called

Since the value when the console.log is called ({value1: 17, value2: 0}), and the current value ({value1: 17, value2: 17}) is different, it prints different thing.

  • Thanks a lot. I made a new change to the object then I expanded the second log (that was already printed beforehand) and I could verify what you explained. – João Pucci Feb 28 '20 at 03:27