DEV Community

Discussion on: Post an Elegant Code Snippet You Love.

Collapse
 
chantal profile image
Chantal

Reversing a string in Javascript looks super simple and easy to me since day one. You could reverse a string in Javascript using a reverse for loop, but we can utilize the powerfull built-in Array methods reverse() and join() to create a one-liner that does the same thing.

const reverseString = (str) => str.split('').reverse().join('');
const reverse = reverseString('javascript');
console.log(reverse); // tpircsavaj
Enter fullscreen mode Exit fullscreen mode