javaScript Array.join("")

hello,
I have a problem here with myArray.join("");
why the first code work when writing myArray.join("") as a return statement and the second code doesn’t work without being a return statement?

const reverseString = function (string) {
    let myArray = string.split("");
    myArray.reverse();
    return myArray.join("");
};
const reverseString = function (string) {
    let myArray = string.split("");
    myArray.reverse();
    myArray.join("");
    return myArray;
};

Please post actual code instead of pictures. Thanks


.join() does not mutate the array. It returns a new string that is created by joining the contents of the array.

thank you so much;
if there is reference for methods which mutate the array and others not please send a link, thanks again :pray:t2:

I just double check on MDN. I Google for “MDN methodname”, so here I searched “MDN array join”

no i didn’t mean that,
I mean I don’t know which methods mutate the original array and which not.
when I was searching there’s no explination for that join(“”) “only return a new string” not mutate the array.
so I’m asking for a reference which explain which methods change the original array and which only return new thing.

MDN will always have that information.

Here it says

The join() method creates and returns a new string

While for splice it says

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place

So “creates and returns a new string” says nothing about changing the array, while “changes the contents of an array … in place” explicitly says that the array is mutated.


Learning how to read and understand documentation is a skill that takes a fair amount of practice.

1 Like

ok thank you so much;
I’m really gratefull. :pray:t2:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.