Function clone

Returns a non-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.
const a = [1, 2, 3];
const b = clone(a);
b[0] = 4;
console.log(a); // [1, 2, 3]
console.log(b); // [4, 2, 3]
  • Type Parameters

    • T

      The type of the value to be cloned.

    Parameters

    • value: T

      The value to be cloned.

    Returns T

    A non-deep copy of the provided value.