Skip to main content
r/javascript icon

r/javascript

members
online

[AskJS] TIL that `console.log` in JavaScript doesn't always print things in the order you'd expect [AskJS] TIL that `console.log` in JavaScript doesn't always print things in the order you'd expect
AskJS

so i was debugging something yesterday and losing my mind because my logs were showing object properties that "shouldn't exist yet" at that point in the code.

turns out when you console.log an object, most browsers don't snapshot it immediately, they just store a reference. by the time you expand it in devtools the object may have already mutated.

const obj = { a: 1 }; console.log(obj); obj.a = 2;

expand that logged object in chrome devtools and you'll probably see a: 2, not a: 1. Fix is kinda simple, just stringify it or spread it:

console.log(JSON.stringify(obj)); // or console.log({ ...obj });

wasted like 30 minutes on this once. hopefully saves someone else the headache (this is mainly a browser devtools thing btw, node usually snapshots correctly)


The everything app, for work. Get everyone working in a single platform designed to manage any type of work.

Over 2+ million teams rely on ClickUp to collaborate and get work done. Join them!

Image The everything app, for work. Get everyone working in a single platform designed to manage any type of work.