What does this code log to the console? Why?
let array1 = [1, 2, 3];
let array2 = array1;
array1[1] = 4;
console.log(array2);
When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more
What does this code log to the console? Why?
let array1 = [1, 2, 3];
let array2 = array1;
array1[1] = 4;
console.log(array2);
The code outputs:
[ 1, 4, 3]
This result demonstrates that array1 and array2 reference the same array: if we change an element using array1, it also changes that element in array2. The opposite is also true: if we change an element in array2, that also changes the element in array1.
This code also demonstrates that assignment of an array to another array doesn't create a new array, but instead copies a reference from the original array (array1 above) into the target array (array2).
> array1[1] = 4
= 4
> array1
= [ 1, 4, 3 ]
> array2
= [ 1, 4, 3 ]
Video Walkthrough
Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .
Submit your solution for LSBot review. Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .