-2

i am getting a strange javascript array behaviour when im pushing a value inside an array which resides in an object. the array in initially empty and then i push a value into it. now the strange behaviour is that, i console log the obj before and after pushing the value. in the console.log before pushing, the array inside the object should be empty but its giving the array after operation in console log.

let myObj = [{key1: 'hello', key2: []}];

console.log(myObj);

myObj[0].key2.push(1);

console.log(myObj); 

both the console logs in the above code give [ { "key1": "hello", "key2": [ 1 ] } ]

How is this possible

5
  • the first console.log gives me [{ key1: "hello", key2: [] }]. Nothing strange about it Commented Sep 1, 2022 at 18:17
  • I am not sure Chris. I tried the same in browser console, I got the same result as op. Commented Sep 1, 2022 at 18:19
  • You need to provide more details. The code you provided does not produce the results you claim. What browser are you using? Are you using your browsers default built-in console or something else to view the log? Commented Sep 1, 2022 at 18:19
  • EssXTee. im using browser built in console on chrome Commented Sep 1, 2022 at 18:25
  • This question is similar to: Object changes even though console.log is before the line that would make the change. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 26 at 9:00

1 Answer 1

3

If your console is a browser, oftentimes the code will execute at a different time than when the logging occurs. In Chrome, where I took this screenshot, a little "i" icon appears that indicates that the value was computed at a later time.

What is likely happening is that the console is displaying the content after it was already updated.

Console logging the same value before and after the change

From what I understand, this is a performance-optimizing feature that allows for the console to store a reference to the object being logged, and will only load its value once the console.log function (i.e., the browser) is asked to do so.

Edit: And to demonstrate this further, using a Node console environment to run your code does in fact produce the results you expected, since it evaluates the object's value during the log synchronously. Ignore the undefineds here:

> let myObj = [{key1: 'hello', key2: []}];

> console.log(myObj);
[ { key1: 'hello', key2: [] } ]

> myObj[0].key2.push(1);

> console.log(myObj); 
[ { key1: 'hello', key2: [ 1 ] } ]
Sign up to request clarification or add additional context in comments.

3 Comments

this means we cannot trust console.log while debugging
See the docs at developer.mozilla.org/en-US/docs/Web/API/Console/log. "Don't use console.log(obj), use console.log(JSON.parse(JSON.stringify(obj)))."
Jay Buckman you should have given this as an answer so that i could mark it as correct. problem solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.