Function deepClone

Returns a deep copy of the provided value in the following way:

  • If the provided value is an array, a shallow copy of the array is returned.
  • If the provided value is an object, a shallow copy of the object is returned.
  • If the provided value is a function, a function that returns the same value is returned.
  • Otherwise, the provided value is returned as-is.

The type of the value to be cloned.

The value to be cloned.

A deep copy of the provided value.

const a = { value: [1, 2, 3] };
const b = clone(a);
b.value[0] = 4;
console.log(a); // { value: [1, 2, 3] }
console.log(b); // { value: [4, 2, 3] }