0

What will the code below output to the console and why?

var arr1 = "john".split('');
var arr2 = arr1.reverse();
var arr3 = "jones".split('');
arr2.push(arr3);
console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));

Out Put will be : "array 1: length=5 last=j,o,n,e,s" "array 2: length=5 last=j,o,n,e,s"

Here's an answer that was posted with this question. However, i don't understand what javascript principle or rules that this is following?

"The reverse() method returns a reference to the array itself (i.e., in this case, arr1). As a result, arr2 is simply a reference to (rather than a copy of) arr1. Therefore, when anything is done to arr2 (i.e., when we invoke arr2.push(arr3);), arr1 will be affected as well since arr1 and arr2 are simply references to the same object."

3
  • 2
    It means that reverse is in-place algorithm. Commented Mar 26, 2015 at 2:48
  • 3
    var x = [1,2,3]; x.reverse(); console.log(x); // => [3,2,1] Note how you didn't have to assign x.reverse() to another variable; instead, the operation was performed on the array. Conversely, operations like .map won't work like that: x.map(function (d) { return d * 2; }); console.log(x); // => [3,2,1], since they don't work off a reference Commented Mar 26, 2015 at 2:53
  • 1
    Difficult to improve on that answer. Array.prototype.reverse reverses the array it's called on, then returns a reference to the array. So where b is an array, the result of var a = b.reverse() is that b is reversed and a is assigned a reference to it, so both a and b reference the same array. Commented Mar 26, 2015 at 2:54

4 Answers 4

4

Let's break it down line by line:

var arr1 = "john".split('');

arr1 now points to the array ['j','o','h','n'].

var arr2 = arr1.reverse();

arr1 and arr2 now point to the same array, which is ['n','h','o','j'].

var arr3 = "jones".split('');

arr3 now points to the array ['j','o','n','e','s'].

arr2.push(arr3);

arr2 and arr1 both now point to the array ['n','h','o','j', [ 'j','o','n','e','s']]. Notice that the elements of arr3 aren't appended to the array pointed to by arr1 and arr2. Instead, the entire array is added as a single element.

console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));

Logs the length of arr1, which is 5 ('n','h','o','j', and the second array). arr1.slice(-1) gets the last element of the arr1 array, which is the array containing 'j','o','n','e','s'. The last line duplicates this, but using arr2, which is just another reference to the same array as arr1.

Sign up to request clarification or add additional context in comments.

Comments

0

This does the following:

var arr1 = "john".split(''); //splits into j, o, h, n
var arr2 = arr1.reverse(); // reverses the array to n, h, o, j
var arr3 = "jones".split(''); // splits into j, o, n, e, s
arr2.push(arr3); // adds arr3 to the end of arr2
console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));

3 Comments

Code–only answers aren't liked, you need to explain your answer in clear, simple language. The comments don't do that.
This doesn't explain why arr3 gets added to both arr1 and arr2.
the comments beside each line explains clearly.
0

I think the reason is that assigning arr1.reverse() to arr2 is like binding it to it. Instead of creating a new instance of arr1, you're still using the same array.

So

arr2.push(arr3);

Is equivalant to

arr1.reverse().push(arr3) 

which naturally affects arr1 when you look at it that way

I could be wrong, but that's what I think is happening

Comments

0

The point to be noticed in this code is line no. 2

var arr2 = arr1.reverse();

This code is saying that reverse the array stored in memory[which arr1 is pointing to] and have arr2 as well point to same array.

So, you end up having a one more reference[arr2] to the array which you were referring to by arr1.

and then you update the same array through this new reference arr2.

arr2.push(arr3);

So, all the time you have one array stored in memory , and two references[arr1 and arr2] to the same. You can update the value stored in that memory by any reference and all the references would be reflecting the changes because they are pointing to same memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.