Object and Arrays – Reference vs Copy

Screenshot of working through the "Object and Arrays - Reference vs Copy" tutorial.

Screenshot of working through the “Object and Arrays – Reference vs Copy” tutorial.

This tutorial worked through the differences between referring to a variable, array or object and making a copy that you can then manipulate without affecting the original. There were a couple key takeaways I found interesting with this exercise.

The first was that if you let one variable be assigned a value, then refer that variable to a new one and then reassign the first variable this doesn’t change the second.

Next I learnt three ways of copying an array. I think my favourite is using the spread syntax as follows:

[code lang=”js”]
const showHosts = [‘Pinacolada Soundsystem’, ‘Fraserhead’, ‘Dave Branton’, ‘Dr Hitchcock’];
const eightKayMembers = […showHosts];
[/code]

Comments are closed.