cloneArray

Creates a clone of an array.

This function receives an array and returns a clone of that array.

Why this is useful? Well, Javascript copies by reference. So if you "copy" an array like this;

let a = [1, 2, 3]
let b = a

Your variable b would only be referencing the variable a. This basically means if a is updated, b gets updated too.

a.push(4)

console.log(b) // [1, 2, 3, 4]

This function copies by value and not by reference.

import { cloneArray } from '1loc'

let a = [1, 2, 3]
let b = cloneArray(a) // [1, 2, 3]

a.push(4)

console.log(b) // [1, 2, 3] (remains unchanged)

Last updated