You see this in your code:
console.log(user);
And the console shows:
{name: "Rahul", age: 21}
Looks fine, right?
But when you're debugging objects, there is something easy to miss.
For example:
console.log(user);
user.name = "Changed";
When you later expand an object in the browser console, you may not always be looking at the exact state you expected at the moment of logging.
If you want a simple snapshot, you can log the object as JSON:
console.log(JSON.stringify(user));
Now you're seeing the serialized value at that point in time.
The bigger lesson?
Don't just ask:
"Did I log the variable?"
Ask:
"Am I actually seeing its value at the moment the bug happened?"
Sometimes debugging isn't about adding more logs.
It's about understanding what your logs are actually showing you.
Try it yourself: log an object, change it immediately afterward, and inspect the console.
You might be surprised.
Top comments (0)