Reversing a string challenge has been one of the oldest code challeges! Let’s see how can we do it in JS.

Using Array Methods:

Steps to follow:

  • Split the string to char arrays.
  • Reverse the char array.
  • At last join the array to a string.

The same thing happens in the following code section:

1
2
3
4
5
6
7
8
const stringToReverse = 'ashkeys';

const reversedString = stringToReverse
.split('') // [ 'a', 's', 'h', 'k', 'e', 'y', 's' ]
.reverse() // [ 's', 'y', 'e', 'k', 'h', 's', 'a' ]
.join('');

console.log(reversedString); // syekhsa

With ES6 spread operator

It is possible to reduce one line from the above code using ES6 syntax! ^_^

1
2
3
4
5
const stringToReverse = 'ashkeys';

const [...charArrayToReverse] = stringToReverse;

console.log(charArrayToReverse.reverse().join('')); // syekhsa