Yogesh Chauhan's Blog

Reverse a String in JavaScript

in JavaScript on September 18, 2020

These solutions are from JavaScript Notes for Professionals.

Solution 1

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

// "olleh"

Note: The above solution only works if does not contain surrogate pairs.

Solution 2: Using Spread operator

function reverseString(str) {
 return [...String(str)].reverse().join('');

console.log(reverseString('hello')); // "olleh"

Solution 3: Make a function using a For loop

function reverse(string) {
 var strRev = "";
 for (var i = string.length - 1; i >= 0; i--) {
 strRev += string[i];
 return strRev;
reverse("hello"); // "olleh"

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add recaptcha version 3 to PHP website?PHPHow to create a circle that follows a cursor using JavaScript and CSS?CSSHow to display and animate image on scroll using JavaScript?JavaScriptHow to insert Bootstrap 4 in Angular 9 app?AngularHow services and dependency injection work in Angular?AngularHow to convert an HTML radio buttons into a toggle switch using CSS?CSSHow to make flexbox items of the same size?CSSHow to get front page or home page ID in WordPress?WordPressClean Form Input With These PHP Functions Before Inserting into DatabasePHPHow JavaScript classes allows us to do less typing (syntactic sugar)?JavaScriptHow to make a web page look good on any device?HTMLHow to check if the element exists using JavaScript and jQuery?JavaScript