1

I am a bit puzzled with the result of console.log(a). Why has the order of the elements in array "a" been reversed? I understand the logic for array "reversed," but can not wrap my head around what happened to array "a."

Example found on Mozilla Developer Network (here).

var a = ['one', 'two', 'three'];
var reversed = a.reverse(); 

console.log(a);        // ['three', 'two', 'one']
console.log(reversed); // ['three', 'two', 'one']
5
  • It was mutated in place and returned, I suspect. Commented Nov 11, 2016 at 16:16
  • I'm voting to close this question as off-topic because the issue it raises is clearly described in readily-available documentation. Commented Nov 11, 2016 at 16:52
  • 2
    @torazaburo I raised this question as I was reading the readily-available documentation on my own and did not understand what is, according to you, "clearly described." I am a newbie and sometimes newbies raise questions on what might seem "clearly described." And if we followed your logic, all the questions on Stack Overflow should be closed as off-topic because the issues they raise should be clearly described in readily-available documentations... Deep down in my mind, this question is clearly ON topic and tremendously helped a newbie (me) to move forward with my learning. Commented Nov 11, 2016 at 21:36
  • Yes, many of the questions on SO should be closed as off-topic because the issues they raise are clearly described in readily-available documentation. SO is not a "we read the documentation for you" service. If you did not understand the words "in place", which occur in the first dozen words of the MDN page for sort, then I suggest you should have gone from there and tried to understand what that means. It's confusing to me, because many of the answers which you claimed "helped" merely regurgitate the documentation with the same "in-place" wording. Commented Nov 12, 2016 at 4:28
  • @torazaburo Please understand that I have never asked for a "read the documentation for me" service... I was reading the documentation, could not wrap my head around part of the concept, did try to figure it out myself, respectfully asked for help, showed gratitude to the few who took some of their precious time to help, and moved forward with my learning. Thanks for your "suggestions," I will chew on them. Have a great day, sir. Commented Nov 12, 2016 at 15:25

2 Answers 2

4

Pay attention on the description:

The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

the in place is the reason why this happens.

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

3 Comments

Read the comment from user torazaburo below the question.
I am not the one who downvoted, I am the one who posted the question and needed help.
@carllecat Thank you for letting me know. I just deleted my comment. I apologize.
3

reverse function will reverse array in-place (it modifies/mutate original array object):

var a = ['one', 'two', 'three'];
a.reverse();
console.log(a);        // ['three', 'two', 'one']

If you don't want to mutate original array you can make a shallow copy before reversing (with slice function):

var reversed = a.slice().reverse();
console.log(a);        // ['one', 'two', 'three']
console.log(reversed); // ['three', 'two', 'one']

Comments

Your Answer

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