Why didn't we use string on str.reverse()

Hey guys, how come using "" is optional in reverse() but required in join() and split()? Also is there a situation where it should not be used?

Thanks

Your code so far


function reverseString(str) {
var splitString = str.split("");
var reverseArray = splitString.reverse();
var combinedString = reverseArray.join("");
  return combinedString;
}

console.log(reverseString("Greetings from Earth")); 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Reverse a String

Link to the challenge:

Hey guys, how come using "" is optional in reverse() but required in join() and split() ? Also is there a situation where it should not be used?

Because for join it has to know what character you want to go in between the joined pieces (in this case nothing). And split needs to know what character you want to split on (in this case an empty string is given so it means to split for everything, as much as you can). Try some other values and see what happens.

For reverse, it doesn’t need any other data - to reverse is to reverse.

2 Likes

I see, so reverse never needs a string right?

reverse does not need an argument to do what it does, which is reverse the order of the items in an array

1 Like

What would that do? reverse just takes an array and reverses it. It doesn’t need any parameters.

1 Like