This function clones an array, creating a new array with the same elements.
JavaScript:
let clone = arr => [...arr];
// Example
let arr = [1, 2, 3]; let clonedArr = clone(arr); console.log(clonedArr); // [1, 2, 3]
let arr2 = ['a', 'b', 'c']; let clonedArr2 = clone(arr2); console.log(clonedArr2); // ['a', 'b', 'c']
TypeScript:
let clone = (arr: T[]): T[] => [...arr];
// Example
let arr = [1, 2, 3]; let clonedArr = clone(arr); console.log(clonedArr); // [1, 2, 3]
let arr2 = ['a', 'b', 'c']; let clonedArr2 = clone(arr2); console.log(clonedArr2); // ['a', 'b', 'c']