JavaScript · Senior · Question 22 of 75

How do you serialize an object with circular references to JSON?

Explanation

JSON.stringify throws TypeError: Converting circular structure to JSON. In a replacer you can skip already-seen objects or replace them with a marker reference (as libraries like flatted do). For copying within JS, structuredClone works - it supports cycles.

Further reading

Problem with a question

JavaScript · Senior · question 22

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz