JavaScript · Middle · Question 31 of 75

Why is structuredClone(obj) better than JSON.parse(JSON.stringify(obj))?

Explanation

The JSON trick turns Date into a string, loses undefined, Map, Set, Infinity and fails on cycles. structuredClone supports more types but doesn't copy functions, DOM nodes or class prototypes - such objects throw or lose their methods.

Further reading

Problem with a question

JavaScript · Middle · question 31

Test yourself

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

Take this quiz