How to Reverse a String in JavaScript

Published: Sunday, June 5, 2022

Greetings, friends! A common interview problem you may see is reversing a string in JavaScript. There are multiple ways to accomplish such a task, but my favorite is this one line of code.

javascript
Copied! ⭐️
const reversed = 'pizza is life'.split('').reverse().join('');

console.log(reversed);
// OUTPUT: efil si azzip

We can even call this method on a palindrome and check if the original word and reversed word are equivalent.

javascript
Copied! ⭐️
const message = 'racecar';

const reversed = message.split('').reverse().join('');

console.log(message === reversed); // true

That's it! That's all it takes to reverse a string! 😲

Resources