One data grid for JavaScript, React, Vue & Angular. Sorting, filtering, virtualization, pinning, and CSV export included. Early access $199 $349 → [Get it now]

How to Clone an Object in JavaScript

As the creator of CoreUI, I’ll show you the most effective ways to clone objects in JavaScript.

You can clone objects in JavaScript using the spread operator (...) for shallow cloning or structuredClone() for deep cloning.

// Shallow clone with spread operator
const originalObject = { name: 'John', age: 30, city: 'New York' }
const clonedObject = { ...originalObject }

// Deep clone with structuredClone (modern browsers)
const nestedObject = {
  user: { name: 'John', preferences: { theme: 'dark' } },
  settings: ['notifications', 'privacy']
}
const deepCloned = structuredClone(nestedObject)

// Alternative deep clone with JSON methods
const jsonCloned = JSON.parse(JSON.stringify(nestedObject))

The spread operator creates a shallow copy, meaning nested objects are still referenced from the original. For shallow cloning, you can also use Object.assign({}, originalObject). When you need to clone nested objects, use structuredClone() which handles complex data types including dates, regular expressions, and circular references. The JSON method is widely supported but has limitations with functions, undefined values, and special objects.

Best Practice Note:

In CoreUI components, we frequently use object cloning for state management and configuration objects. Choose shallow cloning for simple objects and deep cloning when dealing with nested component configurations to prevent unwanted side effects in your UI components.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to set focus on an input field after rendering in React
How to set focus on an input field after rendering in React

Passing props to child components in React function components
Passing props to child components in React function components

How to loop inside React JSX
How to loop inside React JSX

How to validate an email address in JavaScript
How to validate an email address in JavaScript

Answers by CoreUI Core Team