Reverse a String (spoilers)

Hi everyone, somewhat new to programming, first time poster.

This is the solution I came up with for this challenge:

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

reverseString("hello");

My original thought was to just do this, which did not work:

function reverseString(str) {
  str.split("").reverse().join("");
  return str;
}

reverseString("hello");

My question is: why is the first variable (reverse) necessary, and why does the second piece of code not work? Thanks.

Welcome to the forum!

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

The second code doesn’t work, because you don’t save the result of str.split("").reverse().join("");. This line does not change str but returns a value that you will have to save somewhere. Or you can directly return it like this: return str.split("").reverse().join("");