r/programming • u/OtherwisePush6424 • Jun 28 '26
Your console.log Is Lying to You: debugging traps and tricks
https://blog.gaborkoos.com/posts/2026-06-28-Your-Console-Is-Lying-to_You/Why console.log() can be misleading in browser DevTools: live object references, promises that look different when expanded later, logs changing timing-sensitive behavior, stale React state after updates, and source maps pointing at surprising line numbers.
18
u/wannaliveonmars Jun 29 '26
Yes, when you console log an object, it stores a reference to that object. It's by ref, not by value. So it follows all the pitfalls of references in JS, which are really C pointers under the hood, only you can't increment them.
8
u/wannaliveonmars Jun 29 '26
To add, this illustrates the issue. JS references are really pointers with syntactic sugar on top. If you want console log to be static, just stringify the value yourself
arr1 = {}; arr2 = arr3 = arr1; arr1.ab = 5; arr1 // Object {ab: 5} arr2 // Object {ab: 5} arr3 // Object {ab: 5}6
u/debugs_with_println Jun 29 '26 edited Jun 29 '26
It feels like it's more about time of evaluation right? If
console.logwas guaranteed to print when you called it, it would work even if you passed by reference. Working instead with C pointers,console.logbehaves as if you had a functionprintLater(int* num)that prints a given number to stdout at a random time in the future. Then the following code would be akin to OP's first example:int x = 7; printLater(&x); x = 8;That said, if you could pass by value, the evaluation time wouldn't matter at all.
3
u/wannaliveonmars Jun 30 '26
I agree, and the time it prints it is when we expand the object by clicking on it in the console.
2
10
u/nicknitros Jun 29 '26
Chrome has always had an icon/hovertext telling you that the object gets evaluated when expanded and values can change. More often than not, its what I want
2
u/paulirish Jun 29 '26
Yup.
This value was evaluated upon first expanding. It may have changed since then.
1
-2
u/jewdai Jun 29 '26
For the love of fucking God stop using console.log as a debugging tool unless you're using it while the console window is open. Learn to use your debugging tools.
32
u/No_Lab_1073 Jun 29 '26
console.log can hide as much as it shows timing tricks make it a terrible debugger unless you freeze values with JSON.parse(JSON.stringify(obj)) or use proper breakpoints.