elPastor
elPastor

Reputation: 8956

javascript variable with true AND false value?

I have a global dictionary that stores variables in my web app and noticed something very peculiar. When I attempt to change a boolean (true / false) variable, it appears to do it, but doesn't actually do it.

I re-created the issue with this simple jsfiddle. Using Chrome, launch the DevTools and then click on the "Push Me" button. In the console, you'll see that app['active'] shows {active:true}, but if you expand the dictionary by clicking the triangle to the left, it shows active:false. Huh?

enter image description here

I have a feeling this is some core javascript (jQuery?) concept I'm missing, but any help on:

  1. How to make sure the variable is fully updated, and
  2. Why this is happening

...would be appreciated.

Upvotes: 1

Views: 866

Answers (3)

user9152616
user9152616

Reputation:

There is not a standard way of how console.[method] should behave. In Chrome console.log is kind of async. In your case, if you expand the displayed value before clicking the button, and after click the button, then the displayed value will be only { active: true }

Chrome Dev Tools will display in the console the expanded value only when you requested it. That means, the value could have changed at the time you expand the object.

That's why console.log is not considered a reliable way for debugging.

console.log may act differently across Web Browsers (Firefox, Safari, Chrome, ...)

Upvotes: 0

Abana Clara
Abana Clara

Reputation: 4650

Before you log the variable, its original value was true. That is why the log shows true, but when you expand it, the console shows the updated value.

Try to put the log at the bottom, it will log with the updated value false.

Edit: Since you updated the fiddle, the html element will print true because you assigned the text to it before changing the value to false.

Upvotes: 2

Nima
Nima

Reputation: 190

Your code is working as expected, the console log shows that it was set to true when logging, but since you set it to false immediately after, the expansion just shows you the updated value

Upvotes: 1

Related Questions