Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// This causes TypeError: circular structure const a = {}; const b = { ref: a }; a.ref = b; JSON.stringify(a); // TypeError! // Solution: custom replacer that tracks seen objects function safeStringify(obj) { const seen = new WeakSet(); return JSON.stringify(obj, (key, value) => { if (typeof value === "object" && value !== null) { if (seen.has(value)) return "[Circular]"; seen.add(value); } return value; }); }
Result
Open