DEV Community

Cover image for Native cloning is coming to Javascript soon!
James Thomson
James Thomson

Posted on

6 1

Native cloning is coming to Javascript soon!

If you've ever wondered "why doesn't Javascript have a method to deep clone?", you wouldn't be the only one.

For years we've been using various methods or libraries to clone objects and it's never felt quite right. Thankfully, structuredClone is coming to a browser near you! Don't get scared off by the sea of red for browser support. All browsers have implemented it in their nightly releases, so it's on the way.

How do we use it you say? Simple:

const myObj = {
  title: 'Something',
  myNestedObj: {
    title: 'Something else',
    // ... and so on
  }
}
const myDeepCopy = structuredClone(myObj);
Enter fullscreen mode Exit fullscreen mode

That's it. You now have a deep clone of your object. There are some limitations to be aware of though.

As always,
Happy coding! 🤓
Cover photo credit

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs