⊗jsPmObPBR 100 of 504 menu

Passing objects by reference in JavaScript

Arrays and objects are somewhat different from other primitive data types. The difference is that the variable that stores array, does not actually contain it, but simply refers to it.

In practice, this means that when writing an object to another variable, both variables will refer to the same object. Let's try it in practice. Let's have the following object:

let obj1 = {a: 1, b: 2, c: 3};

Assign it from one variable to another:

let obj2 = obj1;

Let's change one of the variables:

obj2.a = '!';

As a result, the changes will be visible in the other variable as well:

console.log(obj1); // {a: '!', b: 2, c: 3}

Without running the code, determine what will be output to the console:

let arr1 = [1, 2, 3]; let arr2 = arr1; arr1[0] = 'a'; console.log(arr2);

Without running the code, determine what will be output to the console:

let arr1 = [1, 2, 3]; let arr2 = arr1; arr1[0] = 'a'; arr2[1] = 'b'; console.log(arr1);

Without running the code, determine what will be output to the console:

let arr1 = [1, 2, 3]; let arr2 = arr1; arr1[0] = 'a'; arr2[0] = 'b'; console.log(arr2);
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline