bhylu.com

Advertisements

JS Interview Prep

Explain a decision using a small working example. Compare copying an array with sharing the same array reference.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

Before you run it

Write down the result you expect and explain which line produces it. Then run the example and compare your reasoning with the result below.

A practical example

JavaScript

const original = [2, 4];
const copy = [...original];
copy.push(6);
console.log(original.length, copy.length);

What happens

The lengths are 2 and 3. Explain why changing the copy leaves the original length alone.