How do we create a deep clone of a value using Lodash?

Overview

In Lodash, we can create a deep clone of a value using the _.cloneDeep() method.

Syntax

_.cloneDeep(value);
Syntax of the _.cloneDeep() method

Parameter value

This method accepts the following parameter value:

  • value: This is the value that needs to be cloned.

Return value

The _.cloneDeep() method returns a deep clone of a given value. This method returns an empty object if the provided value cannot be cloned.

Example

Let’s look at an example of creating a deep clone of a value using the _.cloneDeep() method in the code snippet below:

Console

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Lines 2–5: We create a variable called value and initialize it.
  • Line 8: We create a deep clone of value.
  • Line 11: We print the deep clone to the console.

Free Resources