Hi everyone,
a short question regarding step 9;
Is it a correct argument if we say that
const arr = [1, 2, 3];
const anotherArr = arr;
is the same as
const arr = [1, 2, 3];
const anotherArr = […arr]; ?
Many thanks in advance!
Hi everyone,
a short question regarding step 9;
Is it a correct argument if we say that
const arr = [1, 2, 3];
const anotherArr = arr;
is the same as
const arr = [1, 2, 3];
const anotherArr = […arr]; ?
Many thanks in advance!
these are actually different things.
The first time, you are making another name for arr when you wrote:
const anotherArr = arr
.
If you modify anotherArr[0] for eg. that will also modify arr.
(you’re essentially modifying arr underneath)
But in the 2nd version, you are making a copy of arr.
So if you modify anotherArr after using the rest operator as you did, it will NOT modify arr.
Continuing the discussion from Learn Basic String and Array Methods by Building a Music Player - Step 9:
I see, thank you!
const anotherArr = arr
.
If you modify anotherArr[0] for eg. that will also modify arr.
(you’re essentially modifying arr underneath)
Another question: So if we modify arr[index] directly, does this also modify anotherArr ?
because in the case of direct assignment, these two variables are just two different names for the same array, then yes, they will both reflect the updated array.
Have you looked at learning how to use the browser’s dev tools yet? The console in the browser allows you to write code snippets so you can test out different pieces of code and see what they do. A useful thing for answering this type of question.
I see. As for the dev tools, many thanks for the tip! I didn’t know this is possible. Will definitely have a look on it.
watch this video here as an intro: (It’s for Chrome but you can also look up firefox tools)