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('');
}
reverseString('hello'); 

// "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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
Create a responsive image gallery using CSS gridCSSSome EASY-to-Understand CSS media query examplesCSSHow to Write Complex Search Queries in SQL?SQL/MySQLIntroduction to Angular modules Part 3: NgModules vs JavaScript modules and Angular librariesAngularHow to deploy Angular App on Dreamhost or Amazon S3 or firebase?Angulartransform-origin Property in CSSCSSHow to add a select dropdown in React?React5 Key Principles Of Good Website UsabilityUI/UXDROP DATABASE (remove a database) from PostgresPostgresWordPress: How to print ACF array field values?WordPressSafe Practice to add Links to cross-origin destinationUI/UXHow to check if radio button is checked or not using JavaScript?JavaScript