Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Cloning an Array:
4.2.
JavaScript
4.3.
Cloning an Object:
4.4.
JavaScript
4.5.
Preventing Modification of Original Array:
4.6.
JavaScript
4.7.
Cloning with Primitive Values:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
What's the difference between shallow and deep cloning?
5.2.
When should you use _.clone() over _.cloneDeep()?
5.3.
Does _.clone() work with all types of data?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.clone() Method

Introduction

In JavaScript, working with objects and arrays often requires creating copies of them to prevent unintended side effects caused by modifying the original reference. This is where Lodash's _.clone() method comes into play. 

Lodash _.clone() Method

It creates a shallow copy of a value, which is particularly useful for duplicating objects or arrays without affecting the original data.

Why This Function is Used

The _.clone() function is used to create a shallow copy of objects or arrays. This means that the top-level elements are duplicated, but nested objects or arrays are still referenced. Shallow cloning is typically used when you need to modify a copy of data without altering the original source, especially when dealing with simple objects or arrays where deep nested structures are not a concern.

Syntax, Parameter and Return Value

Syntax: 

_.clone(value)

Parameters:

value (Object|Array): The value to clone.

Return Value: 

Returns the shallow cloned value.

Examples 

Cloning an Array:

  • JavaScript

JavaScript

var _ = require('lodash');

var array = [1, 2, { a: 3 }];

var clonedArray = _.clone(array);

clonedArray[2].a = 4;

console.log(array);

console.log(clonedArray);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[1, 2, { a: 4 }]
[1, 2, { a: 4 }]


Demonstrates shallow cloning of an array. Note that changes to nested objects affect both the original and the clone.

Cloning an Object:

  • JavaScript

JavaScript

var object = { a: 1, b: { c: 2 } };

var clonedObject = _.clone(object);

clonedObject.b.c = 3;

console.log(object);   

console.log(clonedObject);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ a: 1, b: { c: 3 } }
{ a: 1, b: { c: 3 } }


Shows shallow cloning of an object. Changes to nested objects reflect in both the original and the clone.

Preventing Modification of Original Array:

  • JavaScript

JavaScript

var numbers = [1, 2, 3];

var clonedNumbers = _.clone(numbers);

clonedNumbers.push(4);

console.log(numbers);  

console.log(clonedNumbers);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[1, 2, 3]
[1, 2, 3, 4]


An example of cloning an array to modify the clone without affecting the original.

Cloning with Primitive Values:

  • JavaScript

JavaScript

var value = 'Hello';

var clonedValue = _.clone(value);

console.log(clonedValue);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'Hello'


Demonstrates that cloning primitive values (like strings) simply returns the same value.

Frequently Asked Questions

What's the difference between shallow and deep cloning?

Shallow cloning copies the top-level elements, while nested objects or arrays still refer to the same memory location. Deep cloning, on the other hand, creates a completely independent copy at all levels.

When should you use _.clone() over _.cloneDeep()?

Use _.clone() for simple data structures without deep nesting or when you want nested objects to remain linked to the original. Use _.cloneDeep() for complex structures where you need a fully independent copy.

Does _.clone() work with all types of data?

_.clone() works with most types of data, including objects, arrays, and primitives. However, it does not handle functions or certain complex objects like DOM nodes or special JavaScript objects (like Date, RegExp).

Conclusion

Lodash's _.clone() method is an efficient way to create shallow copies of objects or arrays, ensuring that modifications to the copy do not affect the original data. It is ideal for handling simple data structures where deep cloning is not required.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass