Q: 19
Refer to the following array:
Let arr1 = [ 1, 2, 3, 4, 5 ];
Which two lines of code result in a second array, arr2 being created such that arr2 is not
a reference to arr1?
Which two lines of code result in a second array, arr2 being created such that arr2 is not
a reference to arr1?Options
Discussion
A and B for sure. Had something like this in a mock before, slice() and Array.from() both return new arrays so arr2 isn't just referencing arr1. C's just a pointer copy, and D actually mutates arr1 itself. Pretty confident but open to correction.
B tbh. A looks like a decoy here since I thought slice could still reference parts of the original if not careful.
My pick: option D works too since sort() creates a new array, right? I know C just points to the same reference, but isn’t D making a separate one? Let me know if I’m missing something about how sort() behaves.
A and B imo. Both slice(0,5) and Array.from(arr1) give you a new array, not just a reference. C just copies the reference and D mutates the original. If I'm off let me know.
Not C, A and B. Not 100% sure because I get confused with how slice works, can someone confirm?
Be respectful. No spam.