Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

How to create a shallow clone of a value using Lodash

Shubham Singh Kshatriya

Overview

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

Syntax

_.clone(value);
Syntax of _.clone()

Parameters

This method accepts the following parameters:

  • value: This is the value to be cloned.

Return value

This method returns a shallow clone of the value. If the value is not able to be cloned, this method returns an empty object.

Example

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

Console

Explanation

In the HTML tab, we see the following:

  • Line 5: We import the lodash script.

In the JavaScript tab, we see the following:

  • Lines 2–5: We create a variable value and initialize it.
  • Line 8: We create a shallow clone of value.
  • Line 10: We print the shallow clone on the console.

RELATED TAGS

lodash
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya
RELATED COURSES

View all Courses

Keep Exploring