"HTML, CSS, JS" sandbox object error

Objects are changed before time see code below…

let myObj = {name:"none"}
console.log(myObj)
myObj.name="A name"
console.log(myObj)

result “A name” in both console

I don’t know if this is correct but I would assume it is because the console.log function has some async component, which means it gets added to a list of stuff that runs at the end of the event loop, or the start of the next loop. (or something like that :person_shrugging:) And because it only has a reference to the object, and not a copy, it displays the most recent version.
You could possible fix it like this if you wanted.

let myObj = { name: "none" };
console.log(JSON.parse(JSON.stringify(myObj));
myObj.name = "A name";
console.log(myObj);

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.