1

following should make comprehensible to you:

I set value to 1. Then outputting it in the console. Here the output shows 1. After I set value to 2. The same console output shows 2.

How can I avoid this? I try to debug some crazy shit and have to know the values from an object when I am outputting it. Not the latest values from the object. enter image description here

Buntel
  • 540
  • 6
  • 19
  • No, I don't understand your question, please elaborate on the problem you are encountering – Luca Kiebel Jul 20 '17 at 16:38
  • Possible duplicate of [Google Chrome console.log() inconsistency with objects and arrays](https://stackoverflow.com/questions/24175017/google-chrome-console-log-inconsistency-with-objects-and-arrays) – Sebastian Simon Jul 20 '17 at 16:45
  • Maybe you're right @Xufox. I will evaluate it tomorrow. – Buntel Jul 20 '17 at 17:54

2 Answers2

5

When you are console logging it, it is a reference to the variable, so regardless of when you printed it, it's linking to the same value that you've now changed.

You can print it in a string, for example, to see what it was at that point:

console.log("value: " + x);

This should concatenate the value of x as a string so it won't change (on mobile from devRant so can't test)

console.log("value", x) ; 

Will reference the memory of where x is stored, so it will always update with the variable

Honest Objections
  • 773
  • 1
  • 6
  • 13
1

The console outputs the result of your last expression.

For example, if you write 1 and press enter, it will output 1.

The result of your first expression (which is an assignment) is the value of the "test" variable.

The result of your second expression (which is also an assignment) is the value of "test.value", which is 2.

If you want to see the value of your variable, just write "test" and press enter.

bsonntag
  • 11
  • 2
  • I want to see "test.value" before the second assignment which should be 1. But when I assign 2 the console output that I've outputed before the second assignment changes to 2. Now the console says test.value is 2. But at the time it have been written test.value into the console it had the value 1. – Buntel Jul 20 '17 at 22:49