Why console.log shows the wrong value — and how the console "lies" to you

You're chasing a bug. You add console.log(cart) and open the browser console. The collapsed line says {count: 0}. You click the triangle to expand it — and inside it's count: 3.
So which was it? Zero or three?
Here's the uncomfortable truth: the console doesn't always show what was there at the moment of the call. Sometimes it shows what's in the object right now. You're debugging from the wrong picture and looking for the bug in the wrong place. Let's go through three causes, most common first.
Cause 1. The console expands objects "live"
The most common and the sneakiest.
When you log an object or array, the console in Chrome and Chromium-based browsers does two things. It writes the collapsed preview line immediately. But the contents you see on expanding are read at the moment you click — from the same object, which may have changed by then.
const cart = { count: 0 }
console.log(cart) // preview: {count: 0}
cart.count = 3
// expand it in the console — you see count: 3
An object in the console is a reference, not a photo. We covered why in the article on variables: an object is stored by reference, and everyone looking at it sees its current state.
How to check. Next to an expanded object in Chrome there's a small blue "i" icon. Hover over it — the browser honestly warns you: the value was evaluated when you expanded it and may have changed.
How to fix. Log a snapshot, not a reference:
console.log(JSON.stringify(cart)) // a string, frozen forever
console.log(structuredClone(cart)) // a deep copy at call time
JSON.stringify is handy for a quick look. structuredClone keeps the structure, so you can still expand it. Another option is to put debugger there and inspect values while paused: the code is stopped, nothing changes.
Cause 2. The log ran before the data arrived
The classic with requests:
const products = []
fetch('/api/products')
.then(r => r.json())
.then(data => products.push(...data))
console.log(products, products.length)
The console shows [] 0. You expand the array — and there are 20 products! Yet length is 0.
Two things are happening at once. fetch is asynchronous: console.log ran before the server responded, so length is honestly 0 — it's a number, printed immediately. But you expanded the array a couple of seconds later — and per cause 1, the console showed its current contents.
How to check. Look at the numbers and strings next to the object. A number can't "grow up" — it shows the truth at log time. If length is 0 but there's data inside, the log ran too early.
How to fix. Log and use the data where it already exists: inside .then or after await.
const res = await fetch('/api/products')
const products = await res.json()
console.log(products.length) // now it's true
This is a cousin of another common problem — when fetch returns undefined. Same root: the code didn't wait for the response.
Cause 3. Node shows [Object] instead of data
This one happens in the terminal, on the server, in scripts. You log an API response:
console.log(response)
// { user: { profile: { address: [Object] } } }
Where's the address? It's there. Node's console.log only expands nesting two levels deep, and beyond that prints a placeholder [Object] (for arrays — [Array]). We checked in Node 22: the third level was replaced with [Object].
How to fix. Ask it to show everything:
console.dir(response, { depth: null })
console.log(JSON.stringify(response, null, 2))
The first expands the object fully. The second prints it as formatted JSON — easy to copy and hand to the AI to analyze.
When logs aren't enough: pause the code
Sometimes you have twenty logs and the picture still doesn't add up. Then stop the code entirely:
debugger
Write this line where you want to look at the data and open DevTools. The browser freezes exactly there. Hover over any variable — you'll see its value at that very moment. The code is stopped, so nothing has time to change, and the console can't fool you anymore.
The "step" button in the Sources panel runs the next line. That's how you'll see the moment count turned from zero into three. Remember to remove debugger before you publish.
A 10-second check
See something odd in the console — ask yourself three questions:
- Is it an object or array, and did I expand it later? Log
JSON.stringifyand compare. - Is there a request or timer near the log? Most likely the log ran before the data.
- Is this the terminal, showing
[Object]? Useconsole.dirwithdepth: null.
If you're debugging with an AI, send it the JSON.stringify line, not a screenshot of an expanded object. That way it sees what the code saw. More techniques are in the guide on how to debug with AI.
Is this a browser bug?
No, it's by design. Copying every logged object in full would be expensive in memory, so the console keeps a reference and expands it on demand.
Can strings and numbers "change" too?
No. The console prints strings, numbers and booleans right away, as they are. Surprises only happen with objects and arrays.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





