2

hey guys I understand that the following will work for reversing a string passed to the function:

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

reverseString("hello");

however can someone help me understand why the following won't work?

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

2
  • str = str.split(''); str = ... Commented Nov 7, 2015 at 17:51
  • Please allow me some nit-picking and make you aware that this snippet does not work. JavaScript is UTF-16, so things like "Hi! 😁".split('').reverse().join(''); won't give the expected output (if your font doesn't have support: the last chacter is the emoji at U+1F601 which gets translated into two characters which need the correct order to work). So if you want to use it you have to take care that the input is restricted or use a packet made for it, e.g.: npmjs.com/package/esrever which was just the first Google spit out. Commented Nov 7, 2015 at 18:17

4 Answers 4

3

Those functions don't modify the string; strings are immutable. The functions return new values.

So, a statement like

str.split('');

is a valid statement, but it has no net effect because the returned array is ignored. In the first example you quoted, the returned values are used as object contexts from which the subsequent functions are accessed and called. The return statement there returns the result of the last function call in the chain (the .join() call).

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

Comments

1

Try using var , if expected result is to re-define str , set using str = /*new value */

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

console.log(reverseString("hello"))

Comments

1

Firstly, strings are immutable, you can create new required string, using methods like the first one to return value by operating/invoking methods.

The first function calls methods in chain. Meaning, the return value of one function (object/reference types only) to invoke its method to compute new result.

str.split('').reverse().join('');

Here, split returns array and array's reverse method reverses contents in array (index) and finally join method joins the array elements with ' ' as separator.

whereas in second function its just a sequential call of statements. I guess in str.join(' '); there is no function called join in string prototype.

Comments

0

The method calls are chained so that each method uses the return value from the previous. You can do the same in separate statements if you keep the return value so that you can use it in the next statement:

function reverseString(str) {
  var arr = str.split('');
  arr = arr.reverse();
  var result = arr.join('');
  return result;
}

console.log(reverseString("hello"));

Note also that there is a difference between split('') and split(' '). The first one splits between each character while the second one splits at space characters.

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.